Functions
Last Updated: Jan 2026
A function is a block of code that performs a specific task and can be reused multiple times in your program. Functions make your code modular, reusable, and easier to understand.
It is Two Types :
- User-Defined Functions: Created by you.
- Built-in Functions: Provided by the language itself.
User-Defined Functions
Basic Syntax:
return_type function_name(parameter1, parameter2, ...) {
// Function body
return value;//Based on return type(optional)
}
// Calling the function
function_name(argument1, argument2, ...);
- Parameters are placeholders for values that are passed into the function.
- Arguments are values that are passed to the function when it is called.
Non Parameterized Function
A function that doesn't take any parameters is called a non-parameterized function.
void printHello() {
printf("Hello");
}
// OR Use
/*
void printHello(void) {
printf("Hello");
}
*/
int main() {
printHello();
return 0;
}
Note : void is used to indicate that the function doesn't return any value.
Parameterized Function
A function that takes parameters is called a parameterized function.
void sum(int a, int b) {
int result = a + b;
printf("Sum: %d\n", result);
}
int main() {
sum(5, 10);
return 0;
}
Return Type
A function can have a return type that specifies the type of value it returns.
Note : After Return Statement, Function will not run further.
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 10);
printf("Result: %d\n", result);
return 0;
}
Recursive Function
A recursive function is a function that calls itself directly or indirectly to solve a problem.
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int result = factorial(5);
printf("Factorial: %d\n", result);
return 0;
}
Call by Value vs. Call by Reference Function
Call by Value: C's default behavior. The function receives a copy of the argument's data. Changes made inside the function do not affect the original variable.
Call by Reference (via Pointers): Passing the memory address (&var) allows the function to dereference the pointer and modify the original variable directly.
#include <stdio.h>
void modifyByValue(int num) {
num = 99; // Only modifies the local copy
}
void modifyByReference(int *ptr) {
*ptr = 99; // Dereferences address and updates caller's memory
}
int main(void) {
int x = 10;
modifyByValue(x);
printf("After value: %d\n", x); // Output: 10
modifyByReference(&x);
printf("After reference: %d\n", x); // Output: 99
return 0;
}
Function Pointers
Just like variables, compiled function code resides at a specific memory address. A function pointer stores this address, allowing you to call the function indirectly.
Syntax:
returnType (*pointerName)(parameterType1, parameterType2, ...);
#include <stdio.h>
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int main(void) {
// Declare function pointer taking two ints and returning an int
int (*op)(int, int);
op = &add; // Assign function address (& is optional)
printf("10 + 5 = %d\n", op(10, 5)); // Call via pointer
op = subtract;
printf("10 - 5 = %d\n", op(10, 5));
return 0;
}
CallBack Function
A callback function is a function that is passed as an argument to another function and is executed later.
void printHello() {
printf("Hello");
}
void executeFunction(void (*callback)()) {
callback();
}
int main() {
executeFunction(printHello);
return 0;
}
Inline Function
An inline function is a function that is defined directly in the code where it is called.
inline int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 10);
printf("Result: %d\n", result);
return 0;
}
typedef with Function Pointer
Using typedef transforms function pointers into readable, reusable types—essential for callback architectures and dispatch tables.
Simplifying Function Pointer Syntax
returnType (*pointerName)(parameterType1, parameterType2, ...);
#include <stdio.h>
int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }
// Define 'MathOp' as a function pointer type taking two ints and returning an int
typedef int (*MathOp)(int, int);
// High-level function accepting a callback typed via typedef
void executeOperation(int x, int y, MathOp op) {
printf("Result: %d\n", op(x, y));
}
int main(void) {
MathOp operation = add;
executeOperation(10, 20, operation); // Output: 30
operation = multiply;
executeOperation(10, 20, operation); // Output: 200
return 0;
}
