Efficiently Reuse Your Code with Functions in C

Share This Post

What's Your Mal-Function with C Functions?

Get it, because malfunction has function in it… anyways. Functions are a great way to store code that you will frequently use throughout a program. For instance, if we needed to compute our taxes every year, having a function that could do this for in one line of code beats writing out a hundred or so lines every time. Let’s go over how to do this in the C programming language:

Functions are a great tool we can use to compartmentalize our code as well as encourage reusability. For instance consider the following test program that employs pseudocode. We have actually seen pseudocode in earlier activities. It is essentially a means we can describe what code should do without actually writing it out in a specific language. In this example, we illustrate that we have various student grades and would like to write these grades somewhere in a file:

#include <stdio.h>

int main(void)
{
   int student1Grade = 100;
   int student2Grade = 84;
   int student3Grade = 99;
   // ...

   // PSEUDOCODE
   write(file, student1Grade);
   write(file, student2Grade);
   write(file, student3Grade);
   // ...

   return 0;
}

We’ll cover how to actually do this in a couple of lessons, but the idea still stands. It would be very tedious to enter in each grade and write to the same file. What if there was a way we could write a couple of lines of code that could do this for us (foreshadowing…)?

#include <stdio.h>

// Continuing with the PSEUDOCODE example from above:
void write_to_file(FILE *file, int grade)
{
   write(file, grade);
}

int main(void)
{
   student1grade = 100;
   student2grade = 89;
   student3grade = 84;
   // ...
   write_to_file(file, student1grade);
   write_to_file(file, student2grade);
   write_to_file(file, student3grade);
   // ...
   return 0;
}

While this does not reduce the number of lines we have in the program, it makes our code reusable. When we get into arrays next, we can utilize looping to take advantage of this pattern and create a program that will actually automate the process!

Function components

[return type] [function name]([parameter list])

  • Return type: We must specify the data type we are returning. For instance, our main function has an int return type because we return 0 at the end of our program. If we do not need to return any information back, we can use void.
  • Function name: The name of a function should also have the same characteristics of a variable name. They must also be unique from any built-in keywords, variable names and other function names.
  • Parameter list: We can specify a list of variables we expect to receive in the function. Parameters can be referenced in the function without declaration/instantiation because we have already done so in the function definition. Parameter lists look like this: [data type][variable name], [data type][variable name], … NOTE: You can also omit the parameter list with void. You can also have as many parameters as you require by including a comma after the previous parameter. Do not use a comma after the last parameter specified.

These Semicolons Are Getting Confusing

I know right! Let’s do a quick review of where you should or shouldn’t place a semicolon in C:

Now that we have functions, we can reuse our code quite easily! But we still need to write out several function calls for instances like this:

#include <stdio.h>

void some_function(int a)
{
 print("%d\n", a);
}

int main(void)
{
some_function(1)
some_function(2)
// ....
some_function(10000)
return 0;
}

In the next post, we’ll cover how to take care of this issue! If you are enjoying the series so far or thought this post was helpful, be sure to share it with a friend!

That's weird... something went wrong. Please try again.
Welcome to the R U Coding Me Newsletter!

R U Coding Me Newsletter

Subscribe to our newsletter and stay updated.

We use Brevo as our marketing platform. By Clicking below to submit this form, you acknowledge that the information you provided will be transferred to Brevo for processing in accordance with their terms of use

Are You Coding?

If the answer is no, you’re probably missing out on a large opportunity here. And yes, I said the name!

Our FREE developer resources will help you start programming. Additionally, consider applying for mentorship to accelerate you to your career. Click on the Student button below to get started. It will take you to our available courses and any relevant materials to help get you started.

Ready to take your business to the next level? Creating a robust solution in a short amount of time is hard to do if you’ve never done it before. Plus, why should you juggle yet another project with your business? Click on the Business button below if you’re looking to scale your current business online but don’t want to spend a few years to learn how to do it. It will take you to our services so you can see what would best work for you.

More To Explore
Learn C Programming
Multidimensional Arrays in C With Example

FacebookTweetPinLinkedInEmail Wait, You Said There Wouldn’t Be Any More Pointers! No, if you recall from last time, I mentioned there could be pointers that point

Learn C Programming
Efficiently Debugging C Programs

FacebookTweetPinLinkedInEmail What is Debugging? Overview of Debugging Debugging is an art form, rather than a science. Debugging is the act of removing any issues that

R U Coding Me LLC