Chapter 8 – Functions in C Language
Introduction
As programs become larger, writing all the instructions inside the main() function can make the program difficult to understand and maintain.
Functions help us divide a program into smaller, organized, and reusable blocks of code. A function can perform a particular task and can be called whenever that task is required.
For example, instead of writing the same code for calculating a sum several times, we can create one function and call it whenever needed.
In this chapter, you will learn about function declaration, definition, calling, parameters, return values, different types of functions, and recursion.
1. What is a Function?
A function is a named block of code designed to perform a specific task.
A function can:
Accept input through parameters.
Perform some operation.
Return a result.
Be called multiple times.
Example
#include <stdio.h> void message() { printf("Welcome to C Programming"); } int main() { message(); return 0; }
Output
Welcome to C Programming
Here, message() is a user-defined function.
2. Why Do We Use Functions?
Functions make programs easier to develop and manage.
Main advantages
Code Reusability – Write code once and use it multiple times.
Modularity – Divide a large program into smaller parts.
Readability – Programs become easier to understand.
Debugging – Errors can be located more easily.
Maintenance – Changes can be made in a specific function.
Reduced Code Duplication – Repeated code can be placed inside a function.
3. Parts of a Function
A typical function consists of several parts:
return_type function_name(parameters) { statements; }
For example:
int add(int a, int b) { return a + b; }
Here:
| Part | Meaning |
|---|---|
| int | Return type |
| add | Function name |
| int a, int b | Parameters |
| return a + b; | Returned result |
4. Function Declaration
A function declaration tells the compiler about a function before it is used.
Syntax
return_type function_name(parameter_list);
Example:
int add(int, int);
This is also called a function prototype.
5. Function Definition
The function definition contains the actual statements that perform the task.
Example:
int add(int a, int b) { return a + b; }
The function receives two integers and returns their sum.
6. Function Calling
A function is executed when it is called.
Example:
result = add(10, 20);
Here, the function add() receives 10 and 20.
The returned value is stored in result.
7. Complete Function Example
#include <stdio.h> int add(int a, int b); int main() { int result; result = add(10, 20); printf("Sum = %d", result); return 0; } int add(int a, int b) { return a + b; }
Output
Sum = 30
Program Flow
main() ↓ add(10, 20) ↓ 10 + 20 ↓ 30 ↓ Return to main()
8. Types of Functions
Functions in C can broadly be divided into two categories:
1. Library Functions
These are provided by the C standard library.
Examples:
printf() scanf() strlen() sqrt()
2. User-Defined Functions
These are created by the programmer.
Example:
int add(int a, int b) { return a + b; }
9. Library Functions
C provides many ready-to-use functions through standard header files.
Examples:
| Function | Header | Purpose |
|---|---|---|
| printf() | <stdio.h> | Display output |
| scanf() | <stdio.h> | Read formatted input |
| strlen() | <string.h> | Find string length |
| sqrt() | <math.h> | Calculate square root |
| toupper() | <ctype.h> | Convert character to uppercase |
Using library functions saves programming time and avoids rewriting commonly required operations.
10. User-Defined Functions
A programmer can create functions according to the requirements of a program.
Example:
#include <stdio.h> void welcome() { printf("Welcome to GWALNET"); } int main() { welcome(); return 0; }
Output
Welcome to GWALNET
11. Function with No Arguments and No Return Value
A function may not require any input and may not return a value.
Example:
#include <stdio.h> void display() { printf("C Programming"); } int main() { display(); return 0; }
Here:
There are no parameters.
The return type is void.
The function does not return a value.
12. Function with Arguments and No Return Value
A function can receive arguments but return no value.
Example:
#include <stdio.h> void displayNumber(int n) { printf("Number = %d", n); } int main() { displayNumber(25); return 0; }
Output
Number = 25
13. Function with No Arguments but Return Value
A function can return a value without receiving parameters.
Example:
#include <stdio.h> int getNumber() { return 100; } int main() { int n; n = getNumber(); printf("Number = %d", n); return 0; }
Output
Number = 100
14. Function with Arguments and Return Value
This is one of the most commonly used forms.
Example:
#include <stdio.h> int multiply(int a, int b) { return a * b; } int main() { int result; result = multiply(5, 4); printf("Product = %d", result); return 0; }
Output
Product = 20
15. Four Common Function Categories
Functions can therefore be classified based on arguments and return values.
| Type | Arguments | Return Value |
|---|---|---|
| Type 1 | No | No |
| Type 2 | Yes | No |
| Type 3 | No | Yes |
| Type 4 | Yes | Yes |
Examples
No argument, no return:
void display(void);
Argument, no return:
void display(int n);
No argument, return value:
int getNumber(void);
Argument and return value:
int add(int a, int b);
16. Parameters and Arguments
These two terms are related but have different meanings.
Parameters
Variables listed in the function definition are called parameters.
int add(int a, int b)
Here a and b are parameters.
Arguments
Actual values supplied when calling the function are called arguments.
add(10, 20);
Here 10 and 20 are arguments.
17. Return Statement
The return statement sends a value back to the calling function.
Example:
int square(int n) { return n * n; }
If we call:
square(5);
the function returns:
25
18. void Return Type
When a function does not return a value, its return type can be void.
Example:
void message() { printf("Hello"); }
A void function does not need to return a value.
19. Multiple Function Calls
A function can be called more than once.
#include <stdio.h> void message() { printf("Welcome\n"); } int main() { message(); message(); message(); return 0; }
Output
Welcome Welcome Welcome
The same function is reused three times.
20. Function for Finding Square
#include <stdio.h> int square(int n) { return n * n; } int main() { int number = 6; printf("Square = %d", square(number)); return 0; }
Output
Square = 36
21. Function for Finding Even or Odd
Functions can also be used with conditional statements.
#include <stdio.h> void checkEvenOdd(int n) { if(n % 2 == 0) { printf("Even"); } else { printf("Odd"); } } int main() { checkEvenOdd(15); return 0; }
Output
Odd
22. Function to Find Largest Number
#include <stdio.h> int largest(int a, int b) { if(a > b) { return a; } else { return b; } } int main() { printf("Largest = %d", largest(25, 40)); return 0; }
Output
Largest = 40
23. Passing Arrays to Functions
Arrays can be passed to functions.
Example:
#include <stdio.h> void displayArray(int numbers[], int size) { int i; for(i = 0; i < size; i++) { printf("%d ", numbers[i]); } } int main() { int numbers[] = {10, 20, 30, 40, 50}; displayArray(numbers, 5); return 0; }
Output
10 20 30 40 50
Passing arrays to functions is useful when a function needs to process multiple values.
24. Function and Modular Programming
Suppose a program needs to perform several tasks:
Input data
Calculate total
Calculate average
Display result
Instead of putting everything inside main(), we can create separate functions:
main() ├── inputData() ├── calculateTotal() ├── calculateAverage() └── displayResult()
This approach is called modular programming.
It makes a program easier to understand and maintain.
25. Local Variables
A variable declared inside a function is generally a local variable.
Example:
void calculate() { int result = 50; printf("%d", result); }
The variable result belongs to that function's local scope and cannot normally be accessed directly from another function.
26. Global Variables
A variable declared outside all functions is called a global variable.
Example:
#include <stdio.h> int number = 100; void display() { printf("%d", number); } int main() { display(); return 0; }
Global variables can be accessed by functions according to their scope and declaration.
However, excessive use of global variables can make programs harder to understand and maintain.
27. Recursion
A function that calls itself is called a recursive function.
Example:
#include <stdio.h> void countDown(int n) { if(n > 0) { printf("%d ", n); countDown(n - 1); } } int main() { countDown(5); return 0; }
Output
5 4 3 2 1
A recursive function should have a suitable base condition so that the recursive calls eventually stop.
28. Factorial Using Recursion
The factorial of a positive integer can be represented as:
n! = n × (n-1) × (n-2) × ... × 1
For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
A recursive implementation is:
#include <stdio.h> int factorial(int n) { if(n <= 1) { return 1; } return n * factorial(n - 1); } int main() { printf("Factorial = %d", factorial(5)); return 0; }
Output
Factorial = 120
29. Advantages of Functions
Functions provide many benefits:
1. Reusability
The same function can be called multiple times.
2. Modularity
A large program can be divided into smaller sections.
3. Readability
Functions make the purpose of different parts of a program clearer.
4. Easier Debugging
Individual functions can be tested separately.
5. Easier Maintenance
Changes can often be made inside a particular function without rewriting the entire program.
30. Common Mistakes with Functions
Mistake 1: Calling a function incorrectly
The function parameters and supplied arguments should be compatible.
Mistake 2: Missing function declaration
If a function is used before its definition, an appropriate declaration should be provided before its use.
Example:
int add(int a, int b);
Mistake 3: Incorrect return type
If a function is declared to return int, it should return an appropriate integer value.
Mistake 4: Forgetting the return statement
A non-void function should return a value as required by its definition.
Mistake 5: Incorrect base condition in recursion
Without a proper stopping condition, recursive calls may continue until the program runs out of available call-stack space.
31. Function Prototype, Definition and Call
These three concepts are important.
Function Prototype
int add(int, int);
Function Definition
int add(int a, int b) { return a + b; }
Function Call
int result = add(10, 20);
Complete Flow
Prototype ↓ Definition ↓ Function Call ↓ Function Executes ↓ Result Returned
32. Quick Revision Table
| Term | Meaning |
|---|---|
| Function | Reusable block of code |
| Function Prototype | Declaration of a function |
| Function Definition | Actual implementation |
| Function Call | Executes a function |
| Parameter | Variable in function definition |
| Argument | Actual value passed to function |
| return | Sends a value back |
| void | Indicates no return value |
| Recursion | Function calling itself |
| Local Variable | Variable with local scope |
| Global Variable | Variable declared outside functions |
33. Practice MCQs
Question 1
What is a function?
A. A data type
B. A reusable block of code
C. A variable
D. An operator
Answer: B. A reusable block of code
Question 2
Which keyword indicates that a function does not return a value?
A. null
B. empty
C. void
D. zero
Answer: C. void
Question 3
Which statement is used to send a value back from a function?
A. send
B. return
C. break
D. continue
Answer: B. return
Question 4
What are the values passed to a function during a function call called?
A. Parameters
B. Arguments
C. Variables
D. Identifiers
Answer: B. Arguments
Question 5
What is a function that calls itself called?
A. Nested function
B. Recursive function
C. Library function
D. Main function
Answer: B. Recursive function
Question 6
Which of the following is a function prototype?
A. add(10, 20);
B. int add(int, int);
C. return add;
D. function add();
Answer: B. int add(int, int);
34. Programming Exercises
Try writing C programs to:
Create a function to add two numbers.
Create a function to subtract two numbers.
Create a function to multiply two numbers.
Create a function to calculate the square of a number.
Create a function to check whether a number is even or odd.
Create a function to find the largest of two numbers.
Create a function to calculate the factorial of a number.
Create a function to find the sum of elements in an array.
Create a function to find the largest element in an array.
Create a recursive function to calculate factorial.
Create a function to determine whether a number is prime.
Create separate functions for input, calculation, and output in a small student-marks program.
35. Key Points to Remember
A function is a reusable block of code designed for a specific task.
Functions improve program organization and reusability.
A function can accept parameters and return a value.
void is used when a function does not return a value.
A function prototype declares a function before its use.
Arguments are the actual values passed during a function call.
Parameters are variables that receive those values.
Arrays can be passed to functions.
A function can call another function.
A function can also call itself; this is called recursion.
Recursive functions require an appropriate stopping condition.
Chapter Summary
Functions are an important part of structured programming in C. They allow a large program to be divided into smaller and manageable units.
A function can receive data through parameters, perform a particular operation, and optionally return a result. User-defined functions allow programmers to reuse code and improve program organization.
Understanding functions is an important step toward more advanced C concepts such as pointers, structures, dynamic memory, and file handling.
Next Chapter
Chapter 9 – Pointers in C Language
In the next chapter, you will learn about memory addresses, pointers, the address-of operator &, the dereference operator *, and how pointers are used with variables, arrays, and functions.