Part 2 — Chapter 10

Chapter 10: Functions

As programs become larger, writing all code inside main() becomes difficult to manage. Functions help divide a program into smaller, reusable blocks of code that perform specific tasks.

A function executes only when it is called, which improves program organization, readability, and reusability.

1. What is a Function?

A function is a named block of code designed to perform a particular task. Every C program contains at least one function: main().

Functions can accept input values (parameters), process data, and return a value.

Function Syntax

Syntax
C
return_type function_name(parameters)
{
    // function body
}

Function Components

  • Return Type → The data type of the value returned by the function (e.g., int, void).
  • Function Name → The unique identifier used to call the function.
  • Parameters → Input values passed into the function (optional).
  • Function Body → The block of statements executed when the function is called.
Example Program
function_demo.c
C
#include <stdio.h>
 
void displayMessage() // Function declaration and definition
{
    printf("Welcome to C Programming");
}
 
int main()
{
    displayMessage(); // Calling the function
 
    return 0;
}

How It Works

The function displayMessage() is defined before main(). When the function is called inside main(), program control transfers to the function body and executes its statements. After execution completes, control returns back to main().

Console Output
Welcome to C Programming
Execution Flowchart
main() Starts displayMessage() Call Jump to Definition Execute displayMessage() Return to Caller return 0 (End)
2. Parameters and Return Values

Functions can receive input values through parameters and return calculated results back to the caller using the return statement.

Example 1: Function with Parameters
param_demo.c
C
#include <stdio.h>
 
void add(int a, int b) // Accepts two integer parameters
{
    printf("%d", a + b);
}
 
int main()
{
    add(10, 20); // Pass 10 and 20 as arguments
 
    return 0;
}

How It Works

The function add() accepts two integer parameters: a and b. When add(10, 20) is called inside main(), 10 is passed to a, and 20 is passed to b. The function then prints their sum.

Console Output (Example 1)
30
Example 2: Function Returning a Value
return_demo.c
C
#include <stdio.h>
 
int square(int num) // Returns an integer value
{
    return num * num; // Return statement
}
 
int main()
{
    int result;
 
    result = square(5); // Receives the returned value
 
    printf("%d", result);
 
    return 0;
}

How It Works

The function square() takes an integer parameter num and returns num * num using the return statement. Inside main(), the returned value (25) is assigned to the variable result, which is then printed.

Console Output (Example 2)
25

Advantages of Functions

Functions provide major advantages that make programming clean, modular, and scalable:

  • Reduces Code Repetition: Code can be written once and executed multiple times.
  • Improves Readability: Dividing code into logical functions makes programs easier to read.
  • Easier Debugging & Maintenance: If an error occurs, it can be located and fixed within a specific function.
  • Organizes Large Projects: Helps modularize big software projects into individual, well-defined blocks.

Embedded Focus

Functions are heavily used in embedded systems to organize hardware-related tasks into reusable modules, separating driver-level configurations from core business logic.

hardware_drivers.c
C
void read_sensor()
{
    // Low-level hardware configuration to read sensor over I2C
}
 
void control_motor()
{
    // Generate PWM signal to adjust motor speed
}

Embedded firmware often separates drivers, communication protocols, sensor handling, and control loops into different functions for high modularity and robust maintainability.

← Chapter 9: break and continue Chapter 11: Arrays →