Table of Contents
KEY TAKEAWAYS
- Functions break programs into reusable, modular blocks with a clear purpose
- Functions are defined with a return type, name, and parameter list — declarations (prototypes) enable forward reference
- Arguments are passed by value in C — use pointers to modify the original variable from a function
- Recursive functions call themselves but must have a base case to prevent infinite recursion
- The code becomes huge. Same 15 lines being used at multiple places
- Maintaining it becomes very difficult. Any change in logic will require you to make the changes everywhere. Also, there are high chances that you might miss making the changes at some places.
- Debugging it becomes difficult.
int avgTemp = 0;
avgTemp = (temperature1 + temperature2 + temperature3 + temperature4 + temperature5 + temperature6)/6;
printf("The average temperature is %d\n",avgTemp);
if(avgTemp > 30)
{
printf("It is too hotn");
}
else if(avgTemp<=30 && avgTemp > 10)
{
printf("It is moderaten");
}
else
{
printf("It is too coldn");
} Instead, what you can do is to group them together and give them a name as shown below. We have taken all the statements from above, grouped them and given a name calAvgTemp()void calAvgTemp()
{
int avgTemp = 0;
avgTemp = (temperature1 + temperature2 + temperature3 + temperature4 + temperature5 + temperature6)/6;
printf("The average temperature is %d\n",avgTemp);
if(avgTemp > 30)
{
printf("It is too hotn");
}
else if(avgTemp<=30 && avgTemp > 10)
{
printf("It is moderaten");
}
else
{
printf("It is too coldn");
}
}Now, wherever we need these lines, we can just use the function name there. There are many benefits as you can already guess:- Your code size reduces
- Easier to maintain. Any change in logic will require you to change at only one place
- Debugging is easier
Types of Functions in C
There are two types of function in C programming:- Standard library functions
- User-defined functions

Standard library functions
The standard library functions are built-in functions in C programming.These functions are defined in header files. For example,- The
printf()is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in thestdio.hheader file. Hence, to use theprintf()function, we need to include thestdio.hheader file using#include <stdio.h>. - The
sqrt()function calculates the square root of a number. The function is defined in themath.hheader file.
User-defined functions
As we saw in the beginning of this post, we can create our own functions. Such functions are called user-defined functions.How user-defined function works?
#include <stdio.h>
void functionName()
{
... .. ...
... .. ...
}
int main()
{
... .. ...
... .. ...
functionName();
... .. ...
... .. ...
} The execution of a C program begins from the main() function.When the compiler encounters functionName();, control of the program jumps to void functionName()And, the compiler starts executing the codes inside functionName().The control of the program jumps back to the main() function once code inside the function definition is executed.
addNumbers().#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}Function prototype
A function prototype is simply the declaration of a function that specifies function’s name, parameters and return type. It doesn’t contain function body.A function prototype gives information to the compiler that the function may later be used in the program.Syntax of function prototype
returnType functionName(type1 argument1, type2 argument2, ...);In the above example,
int addNumbers(int a, int b); is the function prototype which provides the following information to the compiler:- name of the function is
addNumbers() - return type of the function is
int - two arguments of type
intare passed to the function
main() function. However, it is always a good practice to do so.Calling Functions in C
Control of the program is transferred to the user-defined function by calling it.Syntax of function call
functionName(argument1, argument2, ...);In the above example, the function call is made using
addNumbers(n1, n2); statement inside the main() function.Function definition
Function definition contains the block of code to perform a specific task. In our example, adding two numbers and returning it.Syntax of function definition
returnType functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
}
When a function is called, the control of the program is transferred to the function definition. And, the compiler starts executing the codes inside the body of a function.Passing arguments to Functions in C
In programming, argument refers to the variable passed to the function. In the above example, two variables n1 and n2 are passed during the function call. These parameters are called actual arguments.The parameters a and b accepts the passed arguments in the function definition. These arguments are called formal parameters of the function or formal arguments.
Return Statement
The return statement terminates the execution of a function and returns a value to the calling function. The program control is transferred to the calling function after the return statement.In the above example, the value of the result variable is returned to the main function. The sum variable in themain() function is assigned this value.
Syntax of return statement
return (expression);For example,
return a; return (a+b);Note: – The type of value returned from the function and the return type specified in the function prototype and function definition must match.
Important Points about Functions in C:
Following are some important points related to functions in C.- Function name must be unique
- Keywords cannot be used as function names
- The type of value returned from the function and the return type specified in the function prototype and function definition must match.
- The number of arguments in a function and a function call must match
- The types of arguments in a function and a function call must match
📖 Related: How to Modularize C Programs into Multiple Files

Vivek Bhageria — Lead Firmware R&D Engineer, 12+ years. Ex-Bosch (automotive powertrain), MusicTribe (real-time audio), medical devices. M.Tech BITS Pilani. I write at NerdyElectronics — practical, register-level embedded systems for engineers who want to understand what’s actually happening under the hood.







