Introduction
Functions in C programming language are blocks of code that perform specific tasks. They allow you to divide your program into smaller, manageable units, making it easier to read, understand, and maintain.
By using functions, we can avoid writing the same piece of code multiple times in a particular program. For example, if you are writing a program that needs you to add numbers several times, It is easier to write a function that adds numbers and use it every time you need it. Most programs consist of various functions that are used to perform specific tasks. This practice is known as modularization.
Aspect of Functions
These are the aspects of functions in C programming;
Image from: Sams Teach Yourself C in 24 Hours, Page 46.
Function Type
This is used to specify the data type of the value the function returns. There are functions that do not return any value, hence they use the keyword void
. The standard printf
function is an example of a void function. The main
function below is of int
data type and also returns an int
.
#include <stdio.h>
int main()
{
printf("Hello, World\n");
return (0);
}
Function Name
This is the function's identifier. It is used to call the function whenever it is needed. A function name should follow the naming convention in C programming and should not take any names of the standard C functions.
Function Declaration
This tells the compiler about the function name and how to call the function. A function declaration is defined with the following syntax;
return_type function_name(argument list ...);
When a function is declared without the following implementation, it is known as a function prototype and it is particularly useful when the function is defined in one source file and it is being used in another.
Function Arguments
These are values that are passed to functions. They correspond to the parameters during declaration and definition.
The function square
below calculates the square of a number. It receives one argument i.e. x
and returns its squared value.
float square(float x)
{
return (x * x);
}
The function findMax
below receives two arguments; num1
and num2
and returns the bigger number or zero if both numbers are equal.
int findMax(int num1, int num2)
{
if (num1 > num2)
return (num1);
else if (num2 > num1)
return (num2);
else
return (0);
}
Function Definition
This contains the main statements containing the control flows executed when the function is called. It is the actual implementation of the function and it contains the return value that must be returned from the function.
Function Call
To execute a function, the name is used followed by the required parameters in parentheses. To call the function findMax
above; this syntax is used; findMax(5, 7);
. Below is an example of its implementation;
#include <stdio.h>
int main()
{
int maxNum;
maxNum = findMax(5, 9);
printf("The biggest number between 5 and 9 is %d:\n", maxNum);
return (0);
}
Conclusion
Functions play a crucial role in C programming by encapsulating functionality and improving code organization and reusability. They help in modularizing the code and making it easier to understand and maintain.