
For a C program, all its commands are contained within functions. Each function performs a specific task. There is a special function called main() — this function is the first one executed after the program starts. All other functions are sub-functions of main() (or processes associated with it, such as callback functions), and their function names can be set by the programmer. Each function can only be defined once, but it can be declared and called multiple times as needed.
Function Definition
The general form of a function definition in C language is as follows:
return_type function_name( parameter list )
{
body of the function
}
A function consists of a function header and a function body. Below are all the components of a function:
-
Return Type: A function can return a value. return_type is the data type of the value returned by the function. Some functions perform operations without returning a value; in this case, the return_type is the keyword void.
-
Function Name: This is the actual name of the function. The function name along with the parameter list forms the function signature.
-
Parameters: Parameters act as placeholders. When a function is called, you pass a value to the parameters, known as actual parameters. The parameter list includes the types, order, and number of function parameters. Parameters are optional, meaning a function may not contain parameters.
-
Function Body: The function body contains a set of statements that define the tasks the function performs.
Next, let’s look at an example:
int add(int a, int b){
int result;
result = a + b;
return result;
}

The content between the {} is the function body.
Function Declaration and Calling
Before using a function, it should be declared to inform the compiler of the function’s type: in other words, a declaration describes the interface of a function. The declaration should at least specify the function’s return value type, as shown in the following example:
int add(int a, int b);
In the function declaration, the names of the parameters are not important; only the types of the parameters are required, so the following is also a valid declaration:
int max(int, int);
When creating a C function, you define what the function does and then call the function to complete the defined tasks.
When the program calls a function, control is transferred to the called function. The called function executes the defined tasks, and when the function’s return statement is executed or the end bracket of the function is reached, control is returned to the main program.
When calling a function, pass the required parameters. If the function returns a value, it can be stored.
Next, let’s look at another example:
#include <stdio.h>
/* Function Declaration */
int max(int num1, int num2);
int main()
{
/* Local Variable Definition */
int a = 100;
int b = 200;
int ret;
/* Call function to get the maximum value */
ret = max(a, b);
printf("Max value is : %d\n", ret);
return 0;
}
/* Function returns the larger of two numbers */
int max(int num1, int num2)
{
/* Local Variable Declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
The running result is as follows:

I want to emphasize the function calling part, especially recursive calls.
Recursion is when a function calls itself. In C language, a function can directly or indirectly call itself; indirect calling refers to calling itself in the lower-level function of the recursive function. The recursive relationship is shown in the following diagram:
-
Direct Call

-
Indirect Call

The syntax format is as follows:
void recursion()
{
statements;
... ... ...
recursion(); /* Function calls itself */
... ... ...
}
int main()
{
recursion();
}
Flowchart:

Now let’s look at an example of a function recursive call:
// The following example uses a recursive function to generate the Fibonacci sequence of a given number:
#include <stdio.h>
int fibonacci(int i)
{
if(i == 0)
{
return 0;
}
if(i == 1)
{
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf("%d\t\n", fibonacci(i));
}
return 0;
}
Function running result:

Source: Reprinted from the internet