Lesson 34 of 60 – Functions in C
57%

Functions in C

A function is a named block of code designed to perform a specific task. Functions help divide a large program into smaller, organized, reusable parts.

Note: A function can be called whenever its task is needed. A function may take input through parameters and may return a value to the calling code.

1. What is a Function?

A function is a block of statements that performs a particular task.

void greet()
{
    printf("Hello");
}

Here, greet() is a function that displays a message.

2. Why Use Functions?

Functions provide several benefits:

  • They divide a program into smaller parts.
  • They make code easier to understand.
  • They allow code to be reused.
  • They make testing and debugging easier.
  • They reduce unnecessary repetition.

3. Basic Function Syntax

return_type function_name()
{
    // statements
}

For example:

void display()
{
    printf("Welcome");
}

The function has a return type, a name, parentheses, and a body.

4. Function Name

The function name identifies the function and is used when calling it.

void showMessage()
{
    printf("Hello");
}

Here, showMessage is the function name.

Function names should follow C identifier rules and should clearly describe the task when possible.

5. Return Type

The return type specifies the type of value a function returns.

int add()
{
    return 10 + 20;
}

Here, the function returns an int value.

A function that does not return a value can use void.

6. void Function

A void function does not return a value to its caller.

void greet()
{
    printf("Hello!");
}

The function can be called to perform an action without producing a return value.

7. Calling a Function

Defining a function does not automatically execute it. The function must be called.

#include <stdio.h>

void greet()
{
    printf("Hello!");
}

int main()
{
    greet();

    return 0;
}

The statement greet(); calls the function.

8. Function Definition

A function definition contains the actual statements that perform the task.

void welcome()
{
    printf("Welcome to C programming!");
}

The statements between the braces form the function body.

9. Function Declaration

A function declaration tells the compiler about a function before the function is used.

void greet();

int main()
{
    greet();

    return 0;
}

void greet()
{
    printf("Hello!");
}

The declaration void greet(); appears before main().

10. Function Prototype

A function prototype is a declaration that specifies the function's return type, name, and parameter types.

int add(int, int);

It tells the compiler that an add function exists and accepts two int arguments.

11. Function with No Parameters

A function can be written without parameters.

void display()
{
    printf("Hello World");
}

int main()
{
    display();

    return 0;
}

The function gets all the information it needs from its own body or other accessible program data.

12. Function Returning an Integer

int getNumber()
{
    return 100;
}

int main()
{
    int value = getNumber();

    printf("%d", value);

    return 0;
}

The function returns the integer value 100.

13. Function Returning a Float

float getPrice()
{
    return 99.50f;
}

int main()
{
    float price = getPrice();

    printf("%.2f", price);

    return 0;
}

The function returns a floating-point value.

14. Calling a Function Multiple Times

void greet()
{
    printf("Hello\n");
}

int main()
{
    greet();
    greet();
    greet();

    return 0;
}

The same function can be called multiple times.

15. Function for Addition

int add()
{
    int a = 10;
    int b = 20;

    return a + b;
}

int main()
{
    int result = add();

    printf("Sum = %d", result);

    return 0;
}

The function calculates and returns the sum.

16. Function for Finding Square

int square(int number)
{
    return number * number;
}

int main()
{
    int result = square(5);

    printf("Square = %d", result);

    return 0;
}

This example uses a parameter to receive the number. Parameters will be covered in more detail in the next lesson.

17. Function for Displaying a Message

void showMessage()
{
    printf("Welcome to Soopro Pathshala!\n");
}

int main()
{
    showMessage();

    return 0;
}

A function can be used to keep display-related code separate from main().

18. main() is a Function

The main() function is the entry point of a hosted C program. Program execution begins from main().

int main()
{
    printf("Program starts here.");

    return 0;
}

Other functions can be called from main().

19. Local Variables in Functions

A variable declared inside a function normally has local scope.

void display()
{
    int number = 10;

    printf("%d", number);
}

The variable number is local to the function block and cannot normally be accessed directly from another function.

20. Returning a Calculated Value

int multiply()
{
    int a = 6;
    int b = 7;

    int result = a * b;

    return result;
}

int main()
{
    printf("Result = %d", multiply());

    return 0;
}

The calculated value is returned to the calling code.

21. Functions Improve Program Structure

A large program can be divided into functions according to different tasks.

void inputData()
{
    // input code
}

void calculate()
{
    // calculation code
}

void displayResult()
{
    // output code
}

This approach makes the program easier to organize and maintain.

22. Function Reusability

A function can be reused whenever the same task is required.

int cube(int number)
{
    return number * number * number;
}

int main()
{
    printf("%d\n", cube(2));
    printf("%d\n", cube(3));
    printf("%d\n", cube(4));

    return 0;
}

The same function is called with different values.

23. Common Mistake: Forgetting Function Call

Defining a function does not execute it automatically.

void greet()
{
    printf("Hello");
}

int main()
{
    return 0;
}

Here, greet() is defined but never called, so its body is not executed.

Correct:

int main()
{
    greet();

    return 0;
}

24. Common Mistake: Wrong Return Type

The return type should match the kind of value the function is intended to return.

int getValue()
{
    return 50;
}

Here, int is appropriate because the function returns an integer value.

A function that returns no value should use void.

25. Common Mistake: Missing return Value

A non-void function should return a value as required by its definition and control flow.

int getNumber()
{
    return 100;
}

The return statement supplies the value to the caller.

26. Complete Function Example

#include <stdio.h>

void welcome()
{
    printf("Welcome to C programming!\n");
}

int main()
{
    welcome();

    return 0;
}

This program defines a simple function and calls it from main().

27. Complete Addition Function

#include <stdio.h>

int add(int a, int b)
{
    return a + b;
}

int main()
{
    int result;

    result = add(10, 20);

    printf("Sum = %d", result);

    return 0;
}

The function receives two values and returns their sum.

28. Complete Calculator Functions

#include <stdio.h>

int add(int a, int b)
{
    return a + b;
}

int subtract(int a, int b)
{
    return a - b;
}

int multiply(int a, int b)
{
    return a * b;
}

int main()
{
    printf("Addition = %d\n", add(10, 5));
    printf("Subtraction = %d\n", subtract(10, 5));
    printf("Multiplication = %d\n", multiply(10, 5));

    return 0;
}

Different functions can handle different operations of the same program.

29. Complete Student Result Example

#include <stdio.h>

float calculateAverage(int a, int b, int c)
{
    return (a + b + c) / 3.0f;
}

int main()
{
    int math = 80;
    int english = 75;
    int computer = 90;

    float average;

    average = calculateAverage(
        math,
        english,
        computer
    );

    printf("Average = %.2f", average);

    return 0;
}

The calculation is placed in a separate function, keeping main() easier to understand.

30. Practice Programs Using Functions

Practice the following programs:

  1. Create a function to display your name.
  2. Create a function to print a welcome message.
  3. Create a function to add two numbers.
  4. Create a function to subtract two numbers.
  5. Create a function to multiply two numbers.
  6. Create a function to calculate the square of a number.
  7. Create a function to calculate the cube of a number.
  8. Create a function to calculate the area of a rectangle.
  9. Create a function to calculate the average of three numbers.
  10. Create a simple calculator using separate functions.

Practice creating, declaring, calling, and returning values from functions. Parameters and arguments will be studied in the next lesson.

📌 Key Points

  • A function is a reusable block of code designed for a specific task.
  • Functions help divide large programs into smaller parts.
  • A function has a return type, name, parameters if needed, and body.
  • void is used when a function does not return a value.
  • A function must be called for its body to execute.
  • A function can return values such as int or float.
  • Function declarations or prototypes can appear before a function is used.
  • main() is the entry point of a hosted C program.
  • Functions improve code organization and reusability.
  • Parameters and return values allow functions to communicate with calling code.

🧠 Quick Quiz

Question: What is the main purpose of a function in C?