A function is a reusable block of code that performs a specific task. It helps divide a large program into smaller, manageable parts. A function can accept input values, execute statements, and optionally return a result to the calling code.
- Functions improve code reusability by allowing the same logic to be executed multiple times.
- They make programs easier to read, test, debug, and maintain.
- 2424C provides both standard library functions and user-defined functions.
#include <stdio.h>
// Function definition
int square(int x) {
return x * x;
}
int main(){
// Function call
int result = square(5);
printf("Square of 5 is: %d\n", result);
return 0;
}
Output
Square of 5 is: 25
Explanation: The square() function accepts an integer, multiplies it by itself, and returns the result. In main(), the function is called with the argument 5, and the returned value is displayed.
Syntax
return_type function_name(parameter_list) {
// Function body// If a value is returned
return value;
}

The main components of a function are:
- Return Type: Specifies the type of value returned by the function. Use void if no value is returned.
- Function Name: Identifies the function and follows C identifier naming rules.
- Parameter List: Specifies the inputs accepted by the function. Use void when the function takes no parameters.
- Function Body: Contains the statements executed when the function is called, enclosed within curly braces { }.
- Return Statement: Ends function execution and optionally sends a value back to the caller.
Why Use Functions in C?
Functions help divide a large program into smaller, reusable blocks of code. They improve code organization, readability, testing, and maintenance.

The following are the key benefits of using functions:
- Avoid Repetition: Write a function once and reuse it multiple times instead of repeating the same code.
- Readable Code: Divide complex logic into smaller, meaningful steps that are easier to understand.
- Easy Debugging: Identify and fix errors within individual functions without examining the entire program.
- Modular Design: Break large programs into smaller, independent components, each responsible for a specific task.
- Simpler Testing: Test individual functions separately to verify that they produce the expected results.
- Team Collaboration: Allow multiple developers to work on different functions or modules simultaneously, reducing development complexity.
Function Declaration, Definition, and Calling
Working with functions in C involves three important concepts: declaration, definition, and calling.
1. Function Declaration
A function declaration, also called a function prototype when it specifies parameter types, informs the compiler about the function's name, return type, and parameters before the function is used.
Syntax:
return_type function_name(parameter_list);
Example:
int add(int a, int b);
This declaration indicates that add() accepts two integer parameters and returns an integer.
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;
}
Here, add() calculates the sum of two integers and returns the result.
3. Function Calling
A function call executes a function by using its name followed by parentheses containing the required arguments.
Example:
int result = add(5, 3);
The values 5 and 3 are passed to add(), which returns their sum.
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main(){
// Function call
int result = add(5, 3);
printf("Sum = %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Output
Sum = 8
Explanation: The function declaration allows main() to call add() even though its definition appears later. When add(5, 3) is executed, the function calculates the sum and returns 8.
Why Is Function Declaration Needed in C?
A function declaration is necessary when the compiler needs to know a function's type before encountering its definition.
- It enables functions to be called before their definitions appear in the source file.
- It allows the compiler to check argument types and the function's return type.
- It supports organizing functions across multiple source files through header files.
- If a function is defined before its first use, a separate declaration is generally unnecessary because its definition also serves as a declaration.
Types of Functions in C
Functions in C are broadly classified into two categories based on how they are provided.

1. Library Functions
Library functions are predefined functions provided by the C standard library. They perform commonly required operations such as input/output, mathematical calculations, and string manipulation.
Function | Purpose | Header File |
|---|---|---|
Displays formatted output | <stdio.h> | |
Reads formatted input | <stdio.h> | |
Calculates square root | <math.h> | |
Calculates string length | <string.h> |
The appropriate header file must be included to access the function declarations.
2. User-Defined Functions
User-defined functions are functions that you create yourself to perform specific tasks in your program. Depending on whether they take input or return a value, they can be of four types:
- No arguments, no return value: The function neither takes input nor returns any result.
- Arguments, no return value: The function takes input but does not return anything.
- No arguments, return value: The function does not take input but returns a result.
- Arguments and return value: The function takes input and returns a result.
Each type serves different purposes depending on what the program needs. Using the right type helps make your code more organized and efficient.
Example: Program to demonstrates all four types of user-defined functions.
#include <stdio.h>
// 1. No arguments, no return value
void greet(void){
printf("Hello\n");
}
// 2. Arguments, no return value
void displaySum(int a, int b){
printf("Sum = %d\n", a + b);
}
// 3. No arguments, with return value
int getNumber(void){
return 10;
}
// 4. Arguments, with return value
int multiply(int a, int b){
return a * b;
}
int main() {
greet();
displaySum(5, 3);
printf("Number = %d\n", getNumber());
printf("Product = %d\n", multiply(4, 5));
return 0;
}
Output
Hello Sum = 8 Number = 10 Product = 20
Explanation: Each function demonstrates a different combination of parameters and return values. Functions returning void perform an operation without returning a value, while functions returning int provide a result to the caller.
How Functions Work
When a function is called, program control transfers from the calling function to the called function. After execution finishes, control returns to the caller.

The execution follows these steps:
- The calling function invokes another function with the required arguments.
- The called function receives the argument values through its parameters.
- The statements inside the function body are executed.
- The function returns a value, if applicable, and control transfers back to the caller.
- Execution continues from the point immediately after the function call.
Parameters and Arguments
Parameters and arguments are related but represent different concepts.
- Parameters: Variables declared in a function's definition to receive input values.
- Arguments: Actual values or expressions supplied when calling a function.
int add(int a, int b) { // a and b are parameters
return a + b;}
// Function call
int result = add(10, 20); // 10 and 20 are arguments
In C, function arguments are passed by value. This means the function receives copies of the supplied values. To allow a function to modify variables in the caller, pointers can be passed.
Memory Management of Functions
When a function is called, the system allocates memory for its parameters and local variables in a stack frame, which is automatically released when the function finishes execution.
- Automatic Memory Allocation: Each function call creates a separate stack frame to store its local variables, parameters, and return information.
- Automatic Memory Deallocation: Once the function returns, its stack frame is removed from the call stack, freeing the allocated memory automatically.