A function is a named block of code that performs a specific task. A function can take inputs (called parameters), execute a block of statements, and optionally return a result.
- A function allows you to write a piece of logic once and reuse it wherever needed in the program.
- This helps keep your code clean, organized, easier to understand and manage.
#include <stdio.h>
// function definition
int square(int x)
{
return x * x;
}
int main()
{
// Calling the function
int result = square(5);
printf("Square of 5 is: %d", result);
return 0;
}
Output
Square of 5 is: 25
Syntax

- Return type: Specifies the type of value the function will return. Use void if the function does not return anything.
- Function name: A unique name that identifies the function. It follows the same naming rules as variables.
- Parameter list: A set of input values passed to the function. If the function takes no inputs, this can be left empty or written as void.
- Function body: The block of code that runs when the function is called. It is enclosed in curly braces { }.

Function Declaration Vs Definition
It's important to understand the difference between declaring a function and defining it. Both play different roles in how the compiler understands and uses your function.
Function Declaration
A declaration tells the compiler about the function's name, return type, and parameters before it is actually used. It does not contain the function's body. This is often placed at the top of the program or in a header file.
int add(int a, int b);
Function Definition
A definition provides the actual implementation of the function. It includes the full code or logic that runs when the function is called.
int add(int a, int b) {
return a + b;
}
Calling a Function
Once a function is defined, you can use it by simply calling its name followed by parentheses. This tells the program to execute the code inside that function.
#include <stdio.h>
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
// Function call
int result = add(5, 3);
printf("The sum is: %d", result);
return 0;
}
Output
The sum is: 8
Explanation
- The add() function is called with the values 5 and 3, computes their sum, and returns the result, which is stored in the variable result.
- A function can be called multiple times from main() or other functions, allowing the same code to be reused whenever needed.
- Function calls improve code reusability, reduce duplication, and make programs easier to read, maintain, and debug.
Types of Function in C
In C programming, functions can be grouped into two main categories: library functions and user-defined functions. Based on how they handle input and output, user-defined functions can be further classified into different types.
Library Functions
These are built-in functions provided by C, such as printf(), scanf(),