Paramclasses Academy - Blog

Tutorials Details

←
Post 12 of 15
→

What is Functions in C Programming

What is Functions in C Programming

October 14, 2026

1. Introduction to Functions

A function in C is a reusable block of code designed to perform a specific task. Functions help divide a large program into smaller, manageable parts, making code easier to read, test, debug, and maintain.

For example, if you need to calculate the sum of two numbers several times, you can write a function once and call it whenever needed.

Without a function:

int a = 10, b = 20;
int sum = a + b;
printf("%d\n", sum);

With a function, you can reuse the same calculation whenever required.

In this module, you will learn:

  • What functions are and why they are useful

  • Types of functions in C

  • Function declaration, definition, and calling

  • Parameters and return values

  • Actual and formal arguments

  • Local and global variables

  • Call by value

  • Recursion

  • Practical programs and common mistakes

2. What Is a Function in C?

A function is a named block of statements that performs a particular operation when called.

General syntax

return_type function_name(parameter_list)
{
    // Function body
    return value;
}

The main parts are:

  • Return type: The type of value the function returns, such as int, float, or void.

  • Function name: The identifier used to call the function.

  • Parameter list: Inputs the function receives, if any.

  • Function body: The statements that perform the task.

  • Return statement: Sends a result back to the caller when the function has a non-void return type.

3. Why Do We Use Functions?

Functions provide several advantages:

  1. Code reusability: Write a task once and use it multiple times.

  2. Modularity: Divide a large program into smaller functions.

  3. Readability: Give meaningful names to operations.

  4. Debugging: Test and troubleshoot individual functions.

  5. Maintenance: Update one function instead of repeating changes throughout the program.

Real-world example: In a student management system, separate functions could calculate marks, compute averages, display student details, and check pass/fail status.

4. Types of Functions in C

C functions can broadly be divided into two categories.

1. Library functions

Predefined functions provided by C libraries.

Examples: printf(), scanf(), strlen(), sqrt()

2. User-defined functions

Functions written by the programmer.

Examples: calculateSum(), displayMessage(), findMaximum()

Library functions require the appropriate header files, such as <stdio.h>, <string.h>, or <math.h>.

5. Function Declaration, Definition, and Calling

These are three important concepts in C.

A. Function declaration (prototype)

A declaration tells the compiler the function's name, return type, and parameter types.

int add(int, int);

B. Function definition

A definition contains the actual implementation.

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

C. Function call

A function call executes the function.

int result = add(10, 20);

Complete example

#include <stdio.h>

int add(int, int);

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

int main(void)
{
    int result = add(10, 20);

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

    return 0;
}

Output:

Sum = 30

How it works:

  1. int add(int, int); declares the function.

  2. add() receives two integers.

  3. return a + b; returns their sum.

  4. main() calls the function with 10 and 20.

  5. The returned value is stored in result and displayed.

6. Four Common Function Patterns

Functions can be classified by whether they receive parameters and return a value.

Type 1: No parameters and no return value

#include <stdio.h>

void greet(void)
{
    printf("Welcome to C Programming!\n");
}

int main(void)
{
    greet();

    return 0;
}

Output:

Welcome to C Programming!

void means the function does not return a value, and (void) indicates that it takes no parameters.

Type 2: Parameters but no return value

#include <stdio.h>

void displaySum(int a, int b)
{
    printf("Sum = %d\n", a + b);
}

int main(void)
{
    displaySum(10, 20);

    return 0;
}

Output:

Sum = 30

The function receives two integers and displays their sum directly.

Type 3: No parameters but returns a value

#include <stdio.h>

int getNumber(void)
{
    return 100;
}

int main(void)
{
    int number = getNumber();

    printf("Number = %d\n", number);

    return 0;
}

Output:

Number = 100

The function takes no arguments and returns an integer.

Type 4: Parameters and a return value

#include <stdio.h>

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

int main(void)
{
    int result = multiply(5, 4);

    printf("Product = %d\n", result);

    return 0;
}

Output:

Product = 20

This pattern is especially useful when a function needs input values and must provide a result for further calculations.

7. Parameters and Arguments

Although these terms are related, they have different meanings.

  • Parameters: Variables listed in the function definition or declaration.

  • Arguments: Actual values or expressions passed when calling the function.

Example:

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

int main(void)
{
    int result = add(5, 7);

    return 0;
}

Here:

  • a and b are parameters.

  • 5 and 7 are arguments.

  • result receives the returned value.

8. Return Statement in C

The return statement ends the current function and, when applicable, sends a value back to the caller.

Example:

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

Calling square(4) returns 16.

Important rules:

  • A non-void function should return a value compatible with its return type.

  • A void function does not return a value.

  • Code after an executed return statement is not reached.

  • Returning a value from main() communicates the program's exit status; return 0; conventionally indicates success.

9. Local and Global Variables

Local variable

A local variable is declared inside a function or block and is accessible within its scope.

void showNumber(void)
{
    int number = 10;
    printf("%d\n", number);
}

The variable number is local to showNumber().

Global variable

A global variable is declared outside all functions and can be accessed by functions within its scope, subject to normal declaration rules.

#include <stdio.h>

int count = 100;

void displayCount(void)
{
    printf("%d\n", count);
}

int main(void)
{
    displayCount();

    return 0;
}

Output:

100

Global variables can be useful in some situations, but excessive use can make programs harder to understand and test. Prefer parameters and return values when practical.

10. Call by Value in C

C passes function arguments by value. This means the function receives a copy of the argument's value, so changing a normal parameter does not change the caller's original variable.

Example:

#include <stdio.h>

void changeValue(int number)
{
    number = 50;
}

int main(void)
{
    int value = 10;

    changeValue(value);

    printf("Value = %d\n", value);

    return 0;
}

Output:

Value = 10

Although number becomes 50 inside the function, the original variable value remains 10.

To modify a caller's variable, a function can receive a pointer to that variable and update the value through the pointer.

11. Recursion in C

Recursion occurs when a function calls itself to solve a smaller version of a problem.

A recursive function needs a base case that stops further recursive calls.

Example: Factorial

The factorial of a positive integer \(n\) is the product of all integers from 1 through \(n\), with \(0! = 1\).

#include <stdio.h>

int factorial(int n)
{
    if (n <= 1)
    {
        return 1;
    }

    return n * factorial(n - 1);
}

int main(void)
{
    printf("Factorial = %d\n", factorial(5));

    return 0;
}

Output:

Factorial = 120

For factorial(5), the calls reduce the problem as follows:

factorial(5) = 5 × factorial(4)

factorial(4) = 4 × factorial(3)

factorial(3) = 3 × factorial(2)

factorial(2) = 2 × factorial(1)

factorial(1) = 1

The base case prevents the function from calling itself indefinitely. Recursive calls consume stack space, so recursion depth should be considered for large inputs.

12. Real-World Example: Student Grade Calculator

This program uses functions to calculate the average marks and determine a student's grade.

#include <stdio.h>

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

char calculateGrade(double average)
{
    if (average >= 90)
    {
        return 'A';
    }
    else if (average >= 75)
    {
        return 'B';
    }
    else if (average >= 50)
    {
        return 'C';
    }
    else
    {
        return 'F';
    }
}

int main(void)
{
    int m1, m2, m3;

    printf("Enter marks for three subjects: ");

    if (scanf("%d %d %d", &m1, &m2, &m3) != 3)
    {
        printf("Invalid input.\n");
        return 1;
    }

    if (m1 < 0 || m1 > 100 ||
        m2 < 0 || m2 > 100 ||
        m3 < 0 || m3 > 100)
    {
        printf("Marks must be between 0 and 100.\n");
        return 1;
    }

    double average = calculateAverage(m1, m2, m3);
    char grade = calculateGrade(average);

    printf("Average = %.2f\n", average);
    printf("Grade = %c\n", grade);

    return 0;
}

Example output:

Enter marks for three subjects: 85 90 80
Average = 85.00
Grade = B

This program demonstrates how separate functions can perform separate tasks, making the overall program easier to maintain.

13. Common Mistakes in Functions

Mistake 1: Calling a function without an appropriate declaration

Provide a function prototype before its first call when the definition is not already available earlier in the source.

Mistake 2: Using the wrong return type

If a function returns an integer, declare it with int; if it returns no value, use void.

Mistake 3: Forgetting to return a value

A function that promises a result must return an appropriate value on every path that reaches the end of the function.

Mistake 4: Confusing parameters and arguments

Parameters are variables in the function interface; arguments are the values passed to the call.

Mistake 5: Expecting call by value to change the original variable

Normal parameters receive copies of values. Use pointers when a function must modify a caller's object.

Mistake 6: Writing recursion without a base case

Without a stopping condition, recursive calls may continue until the program exhausts its stack resources.

14. Practical Programs for Practice

Try writing these programs yourself:

  1. Create a function to add two integers.

  2. Create a function to find the square of a number.

  3. Write a function that checks whether a number is even or odd.

  4. Create a function to find the maximum of two numbers.

  5. Write a function to calculate the factorial of a number.

  6. Create a function to check whether a number is prime.

  7. Write a function to calculate the average of three numbers.

  8. Create a recursive function to calculate Fibonacci numbers.

  9. Write a function to swap two values using pointers.

  10. Build a calculator using separate functions for addition, subtraction, multiplication, and division.

15. Interview Questions

Q1. What is a function in C?

A function is a reusable block of code that performs a specific task.

Q2. What is a function prototype?

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

Q3. What is the difference between a declaration and a definition?

A declaration introduces the function interface; a definition provides the implementation.

Q4. What is the difference between parameters and arguments?

Parameters appear in the function interface, while arguments are supplied in a function call.

Q5. What does void mean in a function?

It indicates that the function returns no value, or—in a parameter list such as function(void)—takes no parameters.

Q6. Does C support call by reference directly?

No. C passes arguments by value, but pointers can be used to modify objects owned by the caller.

Q7. What is recursion?

Recursion is when a function calls itself, usually with a base case and a smaller subproblem.

Q8. Why are functions useful?

They improve code reuse, modularity, readability, testing, and maintenance.

16. Quick Revision

  • Functions are reusable blocks of code.

  • A function prototype declares the function interface.

  • A function definition contains the implementation.

  • A function call executes the function.

  • Parameters receive values supplied through arguments.

  • return sends a result to the caller when appropriate.

  • C passes arguments by value.

  • Pointers allow functions to access and modify caller-owned objects.

  • Recursion requires a base case.

  • Good function design separates tasks into clear, focused operations.

17. Homework

  1. Define a function and explain its benefits.

  2. Write a program using a function to add two numbers.

  3. Explain function declaration, definition, and calling.

  4. Write a program to find the factorial of a number using recursion.

  5. Create a function to check whether a number is prime.

  6. Demonstrate call by value with an example.

  7. Write a program that calculates student grades using separate functions.

  8. Explain the difference between local and global variables.

Conclusion

In this module, you learned how to declare, define, and call functions in C, use parameters and return values, understand local and global variables, work with pointers to modify caller-owned data, and implement recursion.

←
Post 12 of 15
→