The functions printf() and scanf() have been used since the first program without any knowledge of how they work inside. That is the purpose of a function: a task is written once, given a name, and then used wherever it is needed. This topic shows how to write functions of one's own.

What Is a Function?

Definition: Function. A named, self-contained block of statements that performs a specific task and can be called from other parts of a program.

Functions are the modules of a C program. Every C program has at least one function, main. Library functions such as printf() and sqrt() are supplied with the compiler. User-defined functions are written by the programmer.

The Three Elements of a User-Defined Function

A user-defined function involves three things.

Definition: Function prototype. A declaration that tells the compiler the name of a function, its return type and the types of its parameters, before the function is used.

The prototype (also called the declaration) is written before main and ends with a semicolon: int square(int n);. With it, the compiler can check that every call supplies the right number and types of arguments.

The function definition contains the actual statements. Its first line, the header, matches the prototype, and its body is enclosed in braces.

The function call uses the function. Writing square(4) transfers control to the function, with the value 4 supplied for n.

Parameters and Arguments

The values supplied in a call are arguments (or actual parameters). The variables in the function header that receive them are parameters (or formal parameters). In C, arguments are passed by value: the function receives copies. Changing a parameter inside the function does not change the caller's variable.

The return Statement

The return statement does two things. It ends the function at once, and it sends a value back to the caller. The type of that value is the return type written before the function name. A function that returns nothing has the return type void; it may use return; with no value, or simply end at its closing brace.

A function can return only one value through return.

Kinds of Functions

Table 4.10 classifies functions by whether they take arguments and whether they return a value.

Table 4.10: Kinds of functions with example prototypes

Arguments Return value Example prototype
No No void showLine(void);
Yes No void showSquare(int n);
No Yes int readMark(void);
Yes Yes int square(int n);

Local Variables

Variables declared inside a function are local to it. They are created when the function is called and destroyed when it returns. Two functions may use the same name for their local variables without any conflict.

Advantages of Functions

Functions avoid repeating the same statements, make programs shorter and clearer, allow each task to be tested separately and let work be reused in other programs.

Example 4.10: Functions with and without a return value

Problem: Write a function that returns the larger of two integers and a void function that displays a line of a given number of stars. Use both in a program.

Solution:

  1. Write the prototypes before main.
  2. larger takes two integers and returns an integer.
  3. showLine takes a count and returns nothing.
#include <stdio.h>

int larger(int a, int b);
void showLine(int count);

int main(void)
{
    int x = 12, y = 19, big;

    big = larger(x, y);
    showLine(20);
    printf("Larger of %d and %d is %d\n", x, y, big);
    showLine(20);
    return 0;
}

int larger(int a, int b)
{
    if (a > b)
    {
        return a;
    }
    return b;
}

void showLine(int count)
{
    int i;

    for (i = 1; i <= count; i++)
    {
        printf("*");
    }
    printf("\n");
}

Output:

********************
Larger of 12 and 19 is 19
********************

Result: larger returns 19, which is stored in big. showLine is called twice, which shows how a function saves the same statements from being written again.

Common mistakes

  • Calling a function before its prototype or definition has appeared.
  • Making the prototype and the definition disagree in return type or parameter types.
  • Expecting a change to a parameter inside a function to alter the caller's variable.

Key takeaways

  • A function is a named block that performs one task.
  • A user-defined function needs a prototype, a definition and a call.
  • Arguments are passed by value; the function works on copies.
  • return ends the function and sends back at most one value; void functions return none.

Practice questions

  1. What information does a function prototype give the compiler?
  2. What is the return type of a function that returns no value?
  3. What is the difference between an argument and a parameter?

Answers to the practice questions

  1. The name of the function, its return type and the types of its parameters.
  2. void.
  3. An argument is the value supplied in the call; a parameter is the variable in the function header that receives it.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV