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!
Founder and CEO of R U Coding Me LLC. Jacob obtained his Bachelor’s of Computer Science at the University of Central Florida. He likes to go to the gym and teach people about technology.