Function in C

A function is a block of statements, which is used to perform a specific task.

A C program has at least one function named main().

Types of functions :

There are two types of function in C

  • Library function
  • User defined function

Library Function :

Library functions are the built-in function in C such as puts()gets()printf()scanf() etc. These are the functions which already have a definition in header files.

User defined function :

C allows programmer to define their own function according to their requirement.

Syntax of function :

return_type function_name (argument list)
{
    Block of code
}

return_type : return types are the type of data that is returned by the function. If nothing is returned then the return type is void.

argument list : argument list contains variables names along with their data types. These arguments are inputs for the function.

Syntax :

#include <stdio.h>

void function_name()
{
    //code
}

int main()
{
    //Calling function
    function_name();
}

Example :

    #include<stdio.h>

    /* function return type is void and doesn't have parameters*/
    void demo()
    {
    printf("Hello World");
    }

    int main()
    {
    /*calling function*/
    demo();
    return 0;
    }

Output :

Hello World

Explanation :

demo() is a user defined function, and in main()demo() function is called so that it can perform its task.

function declaration

A function declaration tells the compiler about a function name. The actual body of the function can be defined separately.

Example :

#include <stdio.h>

/* function declaration */
int sum(int num1, int num2);

int main ()
{
    /* local variable definition */
    int a = 10;
    int b = 20;
    int s;

    /* calling a function to get sum of the numbers */
    s = sum(a, b);

    printf( "Sum of the numbers is : %d\n", s );

    return 0;
}

/* function returning the sum of the two numbers */
int sum(int num1, int num2)
{
    /* local variable declaration */
    int result;

    result = num1 + num2;

    return result;
}

Output :

Sum of the numbers is : 30

Explanation :

The function is declared first and then it is defined. The main() calls the function sum() and passes two arguments so that called function can perform its defined task and return the result.

function parameter

There are two types of arguments/parameters

  • Actual parameters
  • Formal parameters

Actual parameters :

Are parameters that appear in function calls.

Syntax :

Function_name(actual parameters)

Formal parameters :

Are parameters that appear in function declarations.

Syntax :

return_type function_name(formal parameters)

Example of actual parameter and formal parameter :

    #include <stdio.h>

    /* function declaration */
    int sum(int num1, int num2);

    int main ()
    {
    /* local variable definition */
    int a = 10;
    int b = 20;
    int s;

    /* calling a function to get sum of the numbers */
    s = sum(a, b); //Actual Parameter

    printf( "Sum of the numbers is : %d\n", s);

    return 0;
    }

    /* function returning the sum of the two numbers */
    int sum(int num1, int num2) //Formal Parameter
    {
    /* local variable declaration */
    int result;
    result = num1 + num2;
    return result;
    }

Output :

Sum of the numbers is : 30

Explanation :

In calling function – sum(a,b)a and b are actual parameters.
And parameters in function definition – int sum(int num1, int num2)num1 and num2 are formal parameters.

NOTE :

  • Formal parameters are always variables.
  • Actual parameters need not have to be variables. You can use variables, numbers, expressions, or even function calls as actual parameters.