C - Functions


  • A function is a block of statements that performs a special task.
  • All C programs are consists of one or more functions. Functions are the building block of any program.
  • Every C program necessarily contains one function that is main function which is the entry point of the any program.

Why use C functions?

  • If we want to perform a task more than one time in a program then it is not necessary to write code for that task again and again. For this purpose we can create a function for that particular task and then we can call it any number of times when we have to perform that particular task.
  • Functions divide the programs into small modules which increase the readability of the program.
  • We can categorize function in three parts:
    • Function declarations
    • Function definition
    • Function calling

Let us discuss each part of the function in detail

Function Declarations

  • Function declaration includes the functions name, its return type and its parameter list.
  • Function declaration is also known as Function prototype.
  • The basic syntax of declaration of a function is as follows:
  • return-type function-name(parameter-list);
  • In the above syntax return- type is a data type of value return by the function.
  • Function name is a meaningful name given to a function.
  • Parameter-list contains the list of parameters passed to a function. It is optional. It means it is also possible that not any parameter is passed by function.
  • Here is an example of function declaration :
  • int sum ( int x, int y);

Function Definition and Function calling

  • Function definition includes the main body of function and to execute the body of function function calling is used.
  • The basic syntax of function definition is as follows:
  • return_type function_name( parameter list )

    {

    body of the function

    }

  • Basic syntax of function calling is as follows:
  • Function_name(parameter list);
  • We can understand the working of function with the help of following syntax:
  • functions
  • Actual arguments are the arguments of calling function. Here x,y,and z are the actual arguments.
  • Formal arguments are the arguments of called function. here a,b, c are the formal arguments
  • Whenever a function is called, control passes to the definition of called function and the working of calling function is stopped. When the execution of called function is completed then control returns back to calling function and execute the next statement.
  • The values of actual arguments passed by the calling function are received by the formal arguments of called function. The number of actual and formal arguments should be same.
  • The called function operates on the formal arguments and returns a value to calling function with the help of return() statement.
  • Here is an example-program that shows the concept of function:

Example

#include<stdio.h>

#include<conio.h>

void add(int x,int y);//function declaration

int main()

{

int a=5,b=6;

add(a,b);//function calling

getch();

return 0;

}

void add(int x,int y)//function definition

{

int z=x+y;

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

}

Here is the output of the above example:

Output

Addition:11

Passing Argument to function

  • In C there are two ways to passing arguments to the function:
    • Call by value
    • Call by reference

Call By Value

  • In this type of argument passing the copy of actual arguments are passed to the formal arguments.
  • In this case all the operation is done on the formal arguments it does not made any change in the actual argument because formal arguments are the photocopy of the actual arguments.
  • It means that all the changes made in the formal argument are local to the block of called function.
  • Here is a program that shows the concept of Call by value:
#include<stdio.h>

#include<conio.h>

void function(int x,int y);//function declaration

int main()

{

int a=5,b=6;

function(a,b);//function calling

printf("\nIn main a=%d,b=%d",a,b);

getch();

return 0;

}

void function(int a,int b)//function definition

{

a=3;

b=4;

printf("\nIn function a=%d,b=%d",a,b);

}

The output of the above program is as follows:

Output

In function a=3,b=4

In main a=5,b=6

Call By Reference

  • In this type of argument passing instead of value, address of value is passed. So in this case function copies the address of an argument into the formal parameter.
  • In Call by reference formal arguments are works as the pointer that points to the actual arguments so the changes made to the formal arguments are permanent.
  • So in this type changes made to the formal argument also affect the actual argument.
  • Here is an example that shows the concept of Call by reference:
  • #include<stdio.h>

    #include<conio.h>

    int main()

    {

    int a=5,b=6;

    function(&a,&b);//function calling

    printf("\nIn main a=%d,b=%d",a,b);

    getch();

    return 0;

    }

    void function(int *a,int *b)//function definition

    {

    *a=3;

    *b=4;

    printf("\nIn function a=%d,b=%d",*a,*b);

    }

    The output of the above program is as follows:

    Output

    In function a=3,b=4

    In main a=3,b=4
  • Basic syntax of function calling is as follows:
  • Function_name(parameter list);
  • We can understand the working of function with the help of following syntax:
  • functions
  • Actual arguments are the arguments of calling function. Here x,y,and z are the actual arguments.
  • Formal arguments are the arguments of called function. here a,b, c are the formal arguments
  • Whenever a function is called, control passes to the definition of called function and the working of calling function is stopped. When the execution of called function is completed then control returns back to calling function and execute the next statement.
  • The values of actual arguments passed by the calling function are received by the formal arguments of called function. The number of actual and formal arguments should be same.
  • The called function operates on the formal arguments and returns a value to calling function with the help of return() statement.
  • Here is an example-program that shows the concept of function:

Example

#include<stdio.h>

#include<conio.h>

void add(int x,int y);//function declaration

int main()

{

int a=5,b=6;

add(a,b);//function calling

getch();

return 0;

}

void add(int x,int y)//function definition

{

int z=x+y;

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

}

Here is the output of the above example:

Output

Addition:11

Declaration of Constants

  • You have to declare the constant before using it into the program.
  • Constant is declared using the keyword 'const'.
  • Here is the syntax for declaring constant in C++.
  • const data-type constant_name=data_value;
  • Some examples of declaring constant in C++ are given here:
  • const int a=10; const float b=10.34; const char c='a';
  • Here is an example of using constant in C++ program:
  • #include<iostream.h>

    int main()

    {

    const int a=10;/*constant declaration*/

    cout<<"\nvalue of a is:"<<a;

    return 0;

    }

  • The output of the above program is as follows:
value of a is:10