Introduction
In C, a function is a self-contained block of code designed to perform a specific task. Functions increase code modularity, promote reusability, and allow division of work in larger programs. They enable you to break a large program into smaller, manageable chunks.
Function Structure
A function in C typically involves the following components:
-
Function Declaration (Prototype)
-
Function Definition
-
Function Call
1. Function Declaration (Prototype)
A prototype tells the compiler about the function’s return type, name, and parameter list. This is useful when the function is defined after the main() function.
Syntax:
return_type function_name(type1, type2, ...);Example:
int add(int, int); // Prototype2. Function Definition
It provides the actual body of the function — what the function does.
Syntax:
return_type function_name(parameter_list) {
// body of the function
}Example:
int add(int a, int b) {
return a + b;
}3. Function Call
To execute the function, we use a function call, usually from the main() function.
Example:
int main() {
int result = add(5, 10);
printf("%d", result);
return 0;
}Return Type
- A function can return only one value.
- The return type of a function should be the same as the data type of the value to be returned.
- If no value is to be returned, the return type should be
void.
Example:
void greet() {
printf("Hello!");
}Parameters and Arguments
-
Parameters are placeholders defined in the function definition.
-
Arguments are actual values passed when calling the function.
Example:
void display(int x, float y) { // Parameters -> x, y
printf("%d %.2f", x, y);
}
int main() {
display(3, 1.5); // Arguments 3, 1.5
}Parameter Passing Techniques
a) Call by Value
Default in C. A copy of the argument is passed; original value remains unchanged.
void modify(int x) {
x = 10;
}b) Call by Reference (via pointers)
We pass the address of variables using pointers. The ==actual value can be changed==.
void modify(int *x) {
*x = 10;
}Types of Functions
a) User-defined Functions
Functions created by the programmer to perform specific tasks.
b) Library Functions
Predefined in C libraries.
Examples:
-
printf(),scanf()— in<stdio.h> -
sqrt(),pow()— in<math.h>
Recursion in Functions
Recursion is when a function calls itself to solve a problem.
Example:
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}Memory Management
Each function call uses a stack frame on the function call stack. This contains:
-
Local variables
-
Return address
-
Parameters
When the function finishes, its frame is popped off the stack, and memory is released. This is particularly useful when using recursion or back-tracing.
Function Pointers
Used to store addresses of functions and call them dynamically. They essentially point to the starting address of a function in memory. Later, these pointers can be used as regular functions and can be called using the () parenthesis notation.
Example:
void hello() {
printf("Hello");
}
int main() {
void (*fptr)() = hello;
fptr(); // Calls hello()
}