What is a Function in C Programming?

C Function: Function is a collection of statement that perform a specific task. It executes when it is called by its name. A large program is divided into a number of small building block for simplicity and this building block is called function.

We can call a function again and again. The most important features of function is code reusability. The C library provides many predefined functions.

Best Functions C Language| Calling Function With Examples
Best Functions C Language| Calling Function With Examples

    C Programming Function Syntax

    return_type function_name(parameter List)


    C Programming Function Description

    1. Return Type: It is a keyword which indicates that which type of value is being returning by the function.

    If we do not to return any value then we use void keyword in place of return_type.


    2. Function Name: It is the actual name of the function. it is also used at the time of calling the function.


    3. Parameter List: It is the place where we can pass a number of parameter/variables may be used in the program. The value of the parameter can be initialized, or we can pass it from the calling of the function. It is an optional part.


    4. Body: It is the place where the actual code is written to perform the specific task.


    Predefined Functions in C Programming 

    Built-in Function: The function which is predefined in the library is called predefined function.

    Example: printf(),scanf(),clrscr(),getch() are predefined function.

    User Defined Functions in C Programming 

    User-Defined Function: The function which is made by the user is called user defined function.

    Example: add(), sub(), multi() and div(), are the function is user-defined function.

    Key Point About the C Function

    Function Declaration: At this stage the function is declared. void add(); This is a function declaration in which void (no return type) indicates there is no value returning by this function and add is the name of the function.

    Function Definition: This is the place where actual code is written to perform the task.

    Example Function

    void add()

    {

     int x=10;

     int y=30;

     int z=x+y;

      printf("Addition:%d",z);

    }

    This is a function definition and here we can see that code is written to perform the addition task.

    Function Calling: At this stage the function is called. add();

    To call a function just write function just write function name and put semicolon(;) after it.


    Function Complete Example in C

    #include<stdio.h>

    #include<conio.h>

    void add()

    {

     int x=10;

     int y=30;

     int z=x+y;

      printf("Addition:%d",z);

    }


    void main()

    {

    clrscr();

    add();

    getch();

    }


    **********OUTPUT**********

    Addition: 40


    How Many Types of Function are There in C.

    There are four categories of user defined function.


    1. Function with no return type and no parameter.


    2. Function with no return type and with parameter.


    3. Function with return type and no parameter.


    4. Function with return type and with parameter.


    Function with No Return type and No Parameter Example

    The function in which there is no parameter and there is no value returning by that function is called Function with no return type and no parameter.

    #include<stdio.h>

    #include<conio.h>

    void add()

    {

     int x=10;

     int y=30;

     int z=x+y;

      printf("Addition:%d",z);

    }


    void main()

    {

    clrscr();

    add();

    getch();

    }


    **********OUTPUT**********

    Addition: 40


    Function with no return type and with parameter

    The function in which there is some parameter and there is no value returning by that function is called Function with no return type and with parameter.

    #include<stdio.h>

    #include<conio.h>

    void add(int x,int y)

    {


     int z;

     int z=x+y;

      printf("Addition:%d",z);

    }


    void main()

    {

    clrscr();

    add(20,30);

    getch();

    }


    **********OUTPUT**********

    Addition: 50


    In the above example there are two parameters of integer data_type namely x and y there at the time of calling two integer value will be passed in which first will assign to y and second will assign to z.

    Function with return type and no parameter

    The function in which there is no parameter and there is some value returning by that function is called Function with return type and no parameter.

    #include<stdio.h>

    #include<conio.h>

    int add()

    {

     int x=10;

     int y=30;

     int z=x+y;

     return z;

      

    }


    void main()

    {

    clrscr();

    int res=add();

    printf("Addition:%d",res);

    getch();

    }


    **********OUTPUT**********

    Addition: 40

    In the above example there is no parameter, but the function will return an integer value because there is an int keyword in the place of return type and the returned value will assign to variable res.

    Function with return type and with parameter

    The function in which there is some parameter and there is some value returning by that function is called Function with return type and with parameter.

    #include<stdio.h>

    #include<conio.h>

    int add (int x,int y)

    {

     int z;

     int z=x+y;

     return z;

    }


    void main()

    {

    clrscr();

    int res=add(30,20);

    printf("Addition:%d",res);

    getch();

    }


    **********OUTPUT**********

    Addition: 50

    In the above example there are two parameters and the function will return an integer value because there is int keyword in the place of return type and the returned value will assign to variable rs.

    Calling a Function in C Programming

    There are two types of calling a function
    1. Call By Value.
    2. Call By Reference.

    Call By Value:In this type of calling a function a direct value is passed at the time of calling. In call by value the changes made in formal parameters don't reflect in actual parameters.

    #include<stdio.h>

    #include<conio.h>

    void add(int x)

    {

     x=x+100;

    }


    void main()

    {

    clrscr();

    int y=10;

    printf("Before calling y:%d\n",y);

    add(y);

    printf("After calling y:%d",y);

    getch();

    }


    **********OUTPUT**********

    Before calling x:110

    After calling x:110

    In the above example we can see that direct value is passed at the time of calling.
    here y is actual parameter and x is formal parameter.

    Call by Reference: In this type of calling a function the reference of the value is passed at the time of calling. in call by value the changes made in formal parameters reflect in actual parameters.
    Reference is also called address. When the address of data is passed at the time of calling, so it is necessary to use Pointer in the place of parameter.

    #include<stdio.h>

    #include<conio.h>

    void add(int *x)

    {

       *x=*+10;

    }


    void main()

    {

    clrscr();

    int y=10;

    printf("Before calling y:%d\n",y);

    add(&y);

    printf("After calling y:%d",y);

    getch();

    }


    **********OUTPUT**********

    Before calling x:10

    After calling x:20


    Defining Python Functions With Default and Optional Arguments

    In this type of function the function contains a number of parameters with some initial value for: example void sum(int x=10,int y=30). At the time of calling if there is no value is passed . then the default value will be x=10 and y=30,but if value is passed then the value will be x=5 and y=6.

    Function Complete Example in C

    #include<stdio.h>

    #include<conio.h>

    void sum(int x=10,int y=20)

    {

     int res=x+y;

      printf("Addition:%d",res);

    }


    void main()

    {

    clrscr();

    int x=10,y=20;

    printf("Without Value\n");

    sum();

    printf("With Value\n");

    sum(5,8);

    getch();

    }


    **********OUTPUT**********

    Without Value

    Addition: 30

    With Value

    Addition: 40


    What Happens when an Array is Passed to a Function

    In this type of function there is an array in the place of parameter void sum(int ar[5]) and its value is passed at the time of calling.

    #include<stdio.h>

    #include<conio.h>

    void sum(int ar[5])

    {

     int s=0

     for(int i=0;i<5;i++)

     s=s+ar[i];

      printf("Total Sum of Array Element:%d",s);

    }


    void main()

    {

    clrscr();

    int x[5]={10,20,50,40,60};

    sum(x);

    getch();

    }


    **********OUTPUT**********

    Total Sum of Array Element:180


    Multiplication Function with Recursion

    The process of calling a function by itself is called Recursion and the function that calls itself is called a recursive function.

    #include<stdio.h>

    #include<conio.h>

    void factorial(int n,int f)

    {


     if(n>=1)

    f=f*n;

    n--;

    factorial(n,f)

    }

    else

    {

    printf("Factorial=%d",f);

    }


    void main()

    {

    clrscr();

    int x;

    printf("Enter Any Number:");

    scanf("%d",&x);

    factorial(x,1);


    getch();

    }


    **********OUTPUT**********

    Enter Any Number: 6

    Factorial=720