Functions in C Programming
The basic definition of a function in programming is simply a self contained unit of program code designed to accomplish a particular task. You can have functions that can cause actions to take place or functions that can find the value for the program. When a program begins to grow and becomes more complex it becomes very difficult to write the entire program out in one single main()
function. Not only is it difficult to write but it becomes difficult to debug and maintain because nothing would be separated. The advantage of functions is that tasks that the program needs to be done can be divided into several independent subtasks. This will reduce the complexity of your program, it will also save you from having to write duplication code. If you needed to perform the same task multiple times throughout your program you could write the task out in one function and reuse the function when needed instead of re-writing it all out. Without having to duplicate code this makes the code much more easier to be read and understand, it becomes better organized. Separating everything into smaller subtasks can also help when testing because small subtask functions can be tested and debugged much simpler than having to test the entire program. We will be taking a look at how to define functions, invoke them and how to pass and return data from functions.
When using functions the first thing we have to do is to define the function, the same as we would define a variable when first creating it. When you create a function, you specify the function header which defines the return type and the name of the function. The basic syntax looks like this :
Return_type Function_name(parameters)
{
Function Body
}
The Return_type
will tell the compiler what type of value will be returned by this function. This can include any legal type of C and includes enumeration types and pointers as well. The Return_type
can also be type void
which means no value is returned. After the Return_type
is the name of the function that you want to name it. Naming a function is just as important as naming a variable, the name should be meaningful. This helps with the readability of your program, meaningful function names can help others understand what the function is doing or what it is supposed to be doing. There are certain rules when it comes to naming your functions, there are a few parameters that need to be kept in mind for the names. The name of a function can be any legal name, but it can not be any reserved word in C programming (example: int
, double
, sizeof
, and so on). Function names should be unique to your program as well, you should not have two functions named the same thing. It can also not be the same name as any of the library functions. As for the term “legal name” is is the same as that of a variable, it must be a sequence of letters or numbers, but the first character must be a letter or an underscore as well. The name should be relevant to what the actual function does within your program. For example :
#include <stdio.h>void multiplyTwoNumbers(int x, int y)
{
int result = x * y;
printf("The product of %d multiplied by %d is : %d\n", x, y, result);
}int main(void)
{
multiplyTwoNumbers(10, 20);
multiplyTwoNumbers(20, 30);
multiplyTwoNumbers(50, 2); return 0;
}
We created a function called multiplyTwoNumbers
this name is meaningful to what the function does which is multiply two numbers, this is an excuse of good function naming. Also notice that the parameters for our function are in parentheses and separated by commas. multiplyTwoNumbers(int x, int y)
these are the parameters for our function that get passed into the function whenever the function is invoked in the program. The names of the parameters ( x
and y
) are local to the function. What “local to the function” means is that that when you start working with functions you have what are called “Global variables” and “Local Variables”. Local variables are defined inside a function. They are automatically created each time the function is called. The value of any local variable can only be accessed by the function in which it is defined. Global variables are the complete opposite of a local variable. Global variables value can be accessed by any function in the program. When building your programs you want to avoid using global variables though. They become hard to find the location of a bug that is caused by them and once the bug is found it is very difficult to fix the bug. Instead of using global variables use parameters in your functions. If there is too much data to use within the parameters then you can use a struct instead for all the data. Here is an example of both a global variable and a local variable :
#include <stdio.h>int myGlobalVariable = 0; // Global Variable
int main ()
{
int myLocalVariable = 0; // Local Variable
// ***
return 0;
}
*** This location within our main()
function we have access to myGlobalVariable
as well as access to myLocalVariable
. But again using global variables should be avoided and function parameters should be used instead, or a struct.
Once the function has been defined and the functionality has been built then the function can be invoked. You call a function by using the function name followed by the arguments. Once called these arguments that are passed to the function name become the parameters for the function. When the function executes its functionality the values supplied as arguments are used. These arguments need to agree with the type, number, and sequence that are set within the original parameters of the function when it was defined. You can also use a function as the value for a variable ( int functionResult = myFunctionCall()
) and the variable will take the value that is supplied by the function. Using functions within your C program can be very useful not only for you to test and debug your program, by testing individual functions instead of testing the entire program, but it will also make it easier for anybody that will read your code. Instead of writing all the code in a single main()
function save time and keep your program code organized and easily maintainable with well named functions.