Functions in C Programming
Functions in C programming allow code to be modularized and reused, making programs easier to
manage and debug. A function is a block of code that performs a specific task and can be called multiple
times within a program.
1. Types of Functions in C
C functions can be classified into two main types:
1. Library (Built-in) Functions – Functions provided by C libraries (e.g., printf(), scanf(), sqrt(), etc.).
2. User-defined Functions – Functions created by the programmer for specific tasks.
2. Structure of a C Function
A C function typically consists of the following parts:
CopyEdit
return_type function_name(parameter_list) {
// Function body (code to execute)
return value; // Optional, based on return type
3. Function Declaration, Definition, and Call
a) Function Declaration (Prototype)
Introduces the function to the compiler.
Declared before main() if the function definition appears later.
Syntax:
CopyEdit
return_type function_name(parameter_list);
Example:
c
CopyEdit
int add(int, int);
b) Function Definition
Provides the actual implementation of the function.
Example:
CopyEdit
int add(int a, int b) {
return a + b;
c) Function Call
Executes the function and provides arguments (if required).
Example:
CopyEdit
int result = add(5, 3);
4. Example Program Using Functions
CopyEdit
#include <stdio.h>
// Function prototype
int add(int, int);
int main() {
int num1 = 10, num2 = 20;
int sum = add(num1, num2);
printf("Sum: %d\n", sum);
return 0;
// Function definition
int add(int a, int b) {
return a + b;
Output:
makefile
CopyEdit
Sum: 30
5. Types of Function Arguments
a) Function with No Arguments and No Return Value
CopyEdit
#include <stdio.h>
void greet() {
printf("Hello, welcome!\n");
int main() {
greet();
return 0;
Output: Hello, welcome!
b) Function with Arguments but No Return Value
CopyEdit
#include <stdio.h>
void displayMessage(char name[]) {
printf("Hello, %s!\n", name);
int main() {
displayMessage("Alice");
return 0;
Output: Hello, Alice!
c) Function with No Arguments but a Return Value
CopyEdit
#include <stdio.h>
int getNumber() {
return 42;
int main() {
int num = getNumber();
printf("Number: %d\n", num);
return 0;
}
Output: Number: 42
d) Function with Arguments and a Return Value
CopyEdit
#include <stdio.h>
int multiply(int a, int b) {
return a * b;
int main() {
int result = multiply(4, 5);
printf("Product: %d\n", result);
return 0;
Output: Product: 20
6. Parameter Passing in C Functions
C supports two methods of passing arguments:
a) Call by Value (Default Method)
The function gets a copy of the argument.
Changes inside the function do not affect the original variable.
Example:
CopyEdit
#include <stdio.h>
void modify(int x) {
x = 20;
int main() {
int num = 10;
modify(num);
printf("Value of num: %d\n", num); // Original value remains unchanged
return 0;
Output: Value of num: 10
b) Call by Reference (Using Pointers)
The function receives a pointer to the argument.
Changes inside the function affect the original variable.
Example:
CopyEdit
#include <stdio.h>
void modify(int *x) {
*x = 20;
int main() {
int num = 10;
modify(&num);
printf("Value of num: %d\n", num); // Value is modified
return 0;
}
Output: Value of num: 20
7. Recursive Functions in C
A recursive function is a function that calls itself.
Example (Factorial Calculation):
CopyEdit
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
Output: Factorial of 5 is 120
8. Function Scope and Lifetime
1. Local Variables: Declared inside a function, available only within that function.
2. Global Variables: Declared outside all functions, accessible throughout the program.
3. Static Variables: Retain their value across multiple function calls.
Example of a static variable:
c
CopyEdit
#include <stdio.h>
void counter() {
static int count = 0;
count++;
printf("Count: %d\n", count);
int main() {
counter();
counter();
counter();
return 0;
Output:
makefile
CopyEdit
Count: 1
Count: 2
Count: 3
9. Inline Functions (Using Macros)
Instead of writing small functions, macros can be used to reduce function call overhead.
CopyEdit
#define SQUARE(x) ((x) * (x))
int main() {
int result = SQUARE(5);
printf("Square: %d\n", result);
return 0;
Output: Square: 25
10. Advantages of Using Functions in C
Code Reusability: Avoid code repetition.
Modularity: Divide programs into smaller parts.
Maintainability: Easier to debug and maintain.
Improved Readability: Logical structure for better understanding.
Summary of Functions in C
Concept Description Example
Function Declaration Tells compiler about function int sum(int, int);
Function Definition Actual implementation of function int sum(int a, int b) {}
Function Call Invoking the function in main() sum(4, 5);
Call by Value Passing values, original remains unchanged modify(num);
Call by Reference Passing addresses, modifies original modify(&num);
Recursion Function calling itself factorial(n - 1);
Static Variables Retains value across function calls static int count = 0;