Function in c language

  • It ia a collection of statements that performs an specific task.
  • It executes when it is called by its name.
  • A Large program is devided 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 pre-defined functions.

Syntax of Function:

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

return_type

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

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

function_name

1. It is the actual name of the function.

2. It is also used at the time of calling the function.

parameter list

1. it is the place where we can pass a number of parameter/variable.

2. These variables may be used in the program.

3. The value of parameter can be initialized or we can pass it from the calling of function.

Body

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

Key point about the function :-

Function Declaration :- At this stage the function is declared.for example :

void add();This is a function declaration in which void(no return_type) indicates there is no value returning by this fuunction and add is the name of function.

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

example:

void add()
{
int a,b=20,c=30;
a=b+c;
printf("Add = %d",a);
}

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

Function calling: At this stage the function is called. for example add();To call a function just write function name and put semi-colon(;) after it.

#include< stdio.h >
void add();
void add()
{
int a,b=20,c=30;
a=b+c;
printf("Add = %d",a);
}
void main()
{
add();
}

output;

Add = 50

Predefined Function

The function which is predefined in the library is called predefined function. for example printf(),scanf(),clrscr()etc.

Userdefined Function

The function which is made by the user is called userdefined function .for example add(),sub(),mul(),div(){Note :- These are userdefined name. it may diffrent} etc.

Category of Userdefined Function

There are four category of userdefined function

  • Function with no return type and no parameter.
  • Function with no return type and with parameter
  • Function with return type and no parameter.
  • Function with return type and with parameter.

Function with no return type and no parameter

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.

Example of no return type and no parameter :

#include< stdio.h >
void add(); //function declaration
void add() // function definition
{
int a,b=20,c=30;
a=b+c;
printf("Add = %d",a);
}
void main()
{
add(); // function calling
}

output;

Add = 50

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.

Example of no return type and with parameter :

#include< stdio.h >
void add(int b,int c); //function declaration
void add(int b,int c) // function definition
{
int a;
a=b+c;
printf("Add = %d",a);
}
void main()
{
add(20,30); // function calling
}

output;

Add = 50

In the above example ther are two parameter of integer type namely b and c there at the time of calling two integer value will be passed in which first will assign to b and second will assign to c.

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.

Example of function return type and no parameter :

#include< stdio.h >
int add(int b,int c); //function declaration
int add(int b,int c) // function definition
{
int a,b=20,c=30;
a=b+c;
return a;
}
void main()
{
int d=add(); // function calling
printf("Add = %d",d);
}

output;

Add = 50

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.

Example of Function with return type and with parameter:

#include< stdio.h >
int add(int b,int c); //function declaration
int add(int b,int c) // function definition
{
int a,b=20,c=30;
a=b+c;
return a;
}
void main()
{
int d=add(20,30); // function calling
printf("Add = %d",d);
}

output;

Add = 50

In the above example there is two parameter and the function will return integer value because there is int keyword in the place of return type and returned value will assign to variable d

Calling of Function

There are two way to calling a function

  • Call By Value.
  • Call By Reference.

Call By Value

In the type of calling a function 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.

Example of call by value:

#include< stdio.h >
void add(int b) // function definition
{
b=b+10;
return a;
}
void main()
{
int int a= 10;
printf("Before calling a = %d",a);
add(a);
printf("After calling a = %d",a);
}

output;

Before calling a = 10
After calling a = 10

In the above example we can see that direct value is passed at the time of calling. Here a is actual parameter and b is formal parameter.

Call By Refrence

  • In this type of calling a function , the refrence of the value is passed at the time of calling.
  • In call by value the changes made in formal parameters reflect in actual parameters.
  • Refrence is also called address.
  • When the address of data is passed at the time of calling so it is neccessary to use pointer in the place of parameter.

for better understanding see the example below :-

#include< stdio.h >
void add(int *b) // function definition
{
*b = *b + 10; // adding 10 to b
}
void main()
{
int int a= 10;
printf("Before calling a = %d",a);
add(&a);
printf("After calling a = %d",a);
}

output;

Before calling a = 10
After calling a = 20

To know the diffrence between call by value and call by refrence focus on the output of the program.

Passing Array to Function

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

#include< stdio.h >
void add(int ar[5]) // function definition
{
int s=0;
for(int i=0;i<5;i++)
{
s=s+ar[i]; // finding the sum
printf("Total sum of element = %d",s);
}
}
void main()
{
int a[5]={10,20,50,40,60};
add(a); //function calling with array
}

output;

Total sum of element =180

In the above example we can see that there is an array ar[5] in place of parameter and there is an other array x[5]