Module 5: Functions in C
Learning Objectives
By the end of this module, you will be able to:
- Understand what functions are and why they are used.
- Declare and define functions.
- Call functions from the
main()function. - Pass parameters to functions.
- Return values from functions.
- Solve problems using recursion.
- Understand variable scope.
- Learn storage classes in C.
- Understand the concept of inline functions.
- Build programs such as factorial, Fibonacci, and prime number checker.
Introduction
A function is a reusable block of code designed to perform a specific task. Instead of writing the same code multiple times, you can write it once inside a function and call it whenever needed.
Functions make programs:
- Easier to read
- Easier to maintain
- Easier to debug
- Reusable
- Modular
For example, instead of writing addition logic multiple times, you can create one function:
1add()
and call it whenever required.
What is a Function?
A function is a named block of code that performs a specific operation.
Every C program contains at least one function:
1main()
Example:
1#include <stdio.h> 2 3void welcome() 4{ 5 printf("Welcome to C Programming!"); 6} 7 8int main() 9{ 10 welcome(); 11 12 return 0; 13}
Output
1Welcome to C Programming!
Advantages of Functions
Functions provide several benefits:
- Code Reusability
- Better Organization
- Easy Debugging
- Reduced Code Duplication
- Improved Readability
- Modular Programming
- Faster Development
Function Declaration (Function Prototype)
A function declaration tells the compiler:
- Function name
- Return type
- Parameters
It informs the compiler about the function before it is used.
Syntax
1return_type function_name(parameters);
Example
1int add(int, int);
Here,
- Return type →
int - Function name →
add - Parameters → two integers
Function Definition
The function definition contains the actual code executed when the function is called.
Syntax
1return_type function_name(parameters) 2{ 3 // Code 4}
Example
1int add(int a, int b) 2{ 3 return a + b; 4}
Function Call
A function call transfers program control to the function.
Example
1#include <stdio.h> 2 3void display() 4{ 5 printf("Hello World"); 6} 7 8int main() 9{ 10 display(); 11 12 return 0; 13}
Output
1Hello World
Complete Example
1#include <stdio.h> 2 3int square(int); 4 5int square(int number) 6{ 7 return number * number; 8} 9 10int main() 11{ 12 int result = square(5); 13 14 printf("Square = %d", result); 15 16 return 0; 17}
Output
1Square = 25
Types of Functions
Functions are generally classified into two categories:
Library Functions
Provided by the C Standard Library.
Examples
1printf() 2scanf() 3strlen() 4sqrt() 5pow()
User-Defined Functions
Created by the programmer.
Example
1void greeting() 2{ 3 printf("Welcome!"); 4}
Function Parameters
Parameters are variables that receive values from the calling function.
Syntax
1return_type function_name(type parameter)
Example
1void displayAge(int age) 2{ 3 printf("%d", age); 4}
Function call
1displayAge(20);
Output
120
Actual and Formal Parameters
Actual Parameters (Arguments)
Values passed during the function call.
1add(10, 20);
Here,
1020
are actual parameters.
Formal Parameters
Variables defined in the function.
1int add(int a, int b)
Here,
ab
are formal parameters.
Pass by Value
C uses pass by value by default.
A copy of the variable is passed to the function.
Example
1#include <stdio.h> 2 3void change(int x) 4{ 5 x = 100; 6} 7 8int main() 9{ 10 int number = 10; 11 12 change(number); 13 14 printf("%d", number); 15 16 return 0; 17}
Output
110
The original variable remains unchanged.
Return Values
A function can return a value using the return statement.
Example
1#include <stdio.h> 2 3int cube(int n) 4{ 5 return n * n * n; 6} 7 8int main() 9{ 10 int result = cube(3); 11 12 printf("%d", result); 13 14 return 0; 15}
Output
127
Void Functions
Functions that do not return any value use the void keyword.
Example
1void welcome() 2{ 3 printf("Welcome!"); 4}
Recursion
Recursion is a technique where a function calls itself to solve a problem.
Every recursive function must have:
- Base Case
- Recursive Case
Without a base case, recursion continues indefinitely and eventually causes a stack overflow.
Recursive Factorial
Mathematical Formula
15! = 5 × 4 × 3 × 2 × 1
Program
1#include <stdio.h> 2 3int factorial(int n) 4{ 5 if(n == 0) 6 return 1; 7 8 return n * factorial(n - 1); 9} 10 11int main() 12{ 13 printf("%d", factorial(5)); 14 15 return 0; 16}
Output
1120
How Recursion Works
factorial(5)
↓
5 × factorial(4)
↓
4 × factorial(3)
↓
3 × factorial(2)
↓
2 × factorial(1)
↓
1 × factorial(0)
↓
1
Then the function returns back through each call until the final answer is computed.
Variable Scope
Scope defines where a variable can be accessed.
There are two main types:
- Local Scope
- Global Scope
Local Variables
Declared inside a function.
Example
1void demo() 2{ 3 int number = 10; 4}
The variable number cannot be accessed outside demo().
Global Variables
Declared outside all functions.
Example
1#include <stdio.h> 2 3int value = 100; 4 5void display() 6{ 7 printf("%d", value); 8} 9 10int main() 11{ 12 display(); 13 14 return 0; 15}
Output
1100
Global variables can be accessed by all functions in the same source file unless restricted.
Storage Classes
Storage classes determine:
- Scope
- Lifetime
- Visibility
- Memory Location
The four commonly used storage classes are:
autoregisterstaticextern
auto
Default storage class for local variables.
1auto int age = 20;
Usually, the auto keyword is omitted because local variables are automatic by default.
register
Suggests storing the variable in a CPU register for faster access.
1register int counter;
The compiler may ignore this suggestion depending on optimization and hardware.
static
A static local variable retains its value between function calls.
Example
1#include <stdio.h> 2 3void counter() 4{ 5 static int count = 0; 6 7 count++; 8 9 printf("%d\n", count); 10} 11 12int main() 13{ 14 counter(); 15 counter(); 16 counter(); 17 18 return 0; 19}
Output
11 22 33
extern
Used to access global variables defined in another source file.
Example
1extern int total;
extern is commonly used in multi-file C projects.
Inline Functions (Concept)
An inline function suggests to the compiler that the function's code be expanded at the point of the call instead of performing a normal function call.
This can reduce function call overhead for very small functions.
Example (supported by C99 and later):
1inline int square(int x) 2{ 3 return x * x; 4}
Note: The
inlinekeyword is only a suggestion to the compiler. The compiler may choose whether to inline the function.
Practice Project 1: Factorial
1#include <stdio.h> 2 3int factorial(int n) 4{ 5 if(n <= 1) 6 return 1; 7 8 return n * factorial(n - 1); 9} 10 11int main() 12{ 13 int number; 14 15 printf("Enter a number: "); 16 scanf("%d", &number); 17 18 printf("Factorial = %d", factorial(number)); 19 20 return 0; 21}
Practice Project 2: Fibonacci Series
1#include <stdio.h> 2 3int main() 4{ 5 int n, first = 0, second = 1, next; 6 7 printf("Enter number of terms: "); 8 scanf("%d", &n); 9 10 for(int i = 0; i < n; i++) 11 { 12 printf("%d ", first); 13 14 next = first + second; 15 first = second; 16 second = next; 17 } 18 19 return 0; 20}
Output
10 1 1 2 3 5 8 13 21 34
Practice Project 3: Prime Number Checker
1#include <stdio.h> 2 3int isPrime(int number) 4{ 5 if(number <= 1) 6 return 0; 7 8 for(int i = 2; i * i <= number; i++) 9 { 10 if(number % i == 0) 11 return 0; 12 } 13 14 return 1; 15} 16 17int main() 18{ 19 int number; 20 21 printf("Enter a number: "); 22 scanf("%d", &number); 23 24 if(isPrime(number)) 25 printf("%d is a Prime Number.", number); 26 else 27 printf("%d is not a Prime Number.", number); 28 29 return 0; 30}
Common Mistakes to Avoid
- Forgetting to declare a function before calling it.
- Declaring a function with one return type and defining it with another.
- Missing a
returnstatement in a non-voidfunction. - Using recursion without a proper base case.
- Accessing local variables outside their scope.
- Using global variables unnecessarily, making programs harder to maintain.
- Assuming the
inlinekeyword guarantees inlining.
Best Practices
- Give functions meaningful names, such as
calculateArea()orfindMaximum(). - Keep each function focused on a single responsibility.
- Pass only the parameters a function needs.
- Prefer local variables over global variables whenever possible.
- Use recursion only when it makes the solution clearer than an iterative approach.
- Write reusable functions to avoid code duplication.
- Document complex functions with comments explaining their purpose and parameters.
Module Summary
In this module, you learned:
- What functions are and why they improve program structure.
- How to declare, define, and call functions.
- How parameters and return values work.
- The difference between actual and formal parameters.
- How recursion solves problems by allowing a function to call itself.
- The concepts of local and global scope.
- The purpose of storage classes:
auto,register,static, andextern. - The concept of inline functions and when they may improve performance.
- How to apply functions through practical programs such as a factorial calculator, Fibonacci series generator, and prime number checker.
After completing this module, you'll be ready to learn arrays and strings in C, including one-dimensional arrays, two-dimensional arrays, string manipulation, and common string library functions.