Functions in C programming

A function is a collection of statements grouped together to do some specific task. In series of learning C programming, we already used many functions unknowingly. Functions such as - printf(), scanf(), sqrt(), pow() or the most important the main() function. Every C program has at least one function i.e. the main() function.

Why use functions?

A program is a collection of small tasks that may depend on other tasks. You can use two approaches to write a program.

  1. Write code for all tasks in single place, in the main() function sequentially.
  2. Divide code for all tasks into separate functions. Use main() function to execute all other tasks.

Think for a while how painful it would have been to write all our code in single file or single function. Hence you would choose the second approach to write a program.

Advantages of dividing program into functions

  • Reusability of code. Functions once defined can be used any several times. You can use functions of one program in another program. It saves time and effort.
  • Functions provides abstraction. To use any function you only need is name and arguments it accepts. You need not to know how it works internally. For example - You have used printf() function hundreds of time. Have you ever cared how printf() works or what it does internally? No, because it’s not worth.
  • Function allows modular design of code. We can divide program into small modules. Modular programming leads to better code readability, maintenance and reusability.
  • It is easier to write programs using functions. You can write code for separate task individually in separate function.
  • Code maintenance and debugging is easier. In case of errors in a function, you only need to debug that particular function instead of debugging entire program.

Function declaration

Like variable declarations, functions are also declared. Function declaration tells the compiler that there exists a function with some name that may be used later in the program. Function declaration is also known as function prototype or function signature.

Syntax of function declaration

return_type function_name( parameter_list );
  • Return type - Return type defines the data type of value returned by the function.

    A function does some calculation and may return a resultant value. So that the result of one function can be used by another function. For example - sqrt() function returns square root of given number, which is later used by calling function.

    You must mention return type as void if your function does not return any value.

  • Function name - Function name is a valid C identifier that uniquely identifies the function. You must follow identifier naming rules while naming a function.
  • Parameter list - A function may accept input. Parameter list contains input type and variable name given to the function. Multiple inputs are separated using comma ,.

A function declaration must be terminated using semicolon ;. You can declare a function anywhere in the program. However it is best practice to declare functions below pre-processor directives.

Important note: Function declaration is optional. You can directly define and call a function skipping its declaration. However, you must define the function prior to its use.

Function definition

Function definition contains block of code to do task that the function is intended to do. Block of code must contain a return statement if return type of the function is not void.

Syntax of function definition

return_type function_name(parameter list)
{
    // Function body
}

Function declaration and definition syntax must be same. You are free to put function definition anywhere in your program, below its declaration. However, I recommend you to put all declarations below main() function.

Function calling

The most important part of function is its calling. Calling of a function in general is execution of the function. It transfers program control from driver (current) function to called function. We can optionally pass input to the calling function.

Syntax of function call

function_name( parameter_list );
  • Function name - Name of the function to execute.
  • Parameter list - Comma separated input given to the function. Parameter list must match type and order mentioned in function declaration. Leave parameter list as blank if function does not accept any input.

Function example program

Let us write a C program to input two numbers from user. Find sum of the given two number using function.

#include <stdio.h>

/* Addition function declaration */
int add(int num1, int num2);


/* Main function definition */
int main()
{
    /* Variable declaration */
    int n1, n2, sum;

    /* Input two numbers from user */
    printf("Enter two numbers: ");
    scanf("%d%d", &n1, &n2);

    /*
     * Addition function call.
     * n1 and n2 are parameters passed to add function.
     * Value returned by add() is stored in sum.
     */
    sum = add(n1, n2);

    /* Print value of sum */
    printf("Sum = %d", sum);

    return 0;
}

/**
 * Addition function definition.
 *
 * Return type of the function is int.
 * num1 - First parameter of the function of int type.
 * num2 - Second parameter of the function of int type.
 */
int add(int num1, int num2)
{
    int s = num1 + num2;

    /* Return value of sum to the main function */
    return s;
}

Output -

Enter two numbers: 10 20
Sum = 30

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>