Function, Syntax of Function,Types of function with examples

 

Function

1.It is a collection of statements that performs an specific task.
2.It executes when it is called by its name.
3.A large program is divided into a number of small building block for simplicity and this building block is called function.
4.We can call a function again and again.
5.The most important features of function is code reusability.
6.The C library provides many pre-defined functions.

Syntax of Function


 Description
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.
4.It is optional part.
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 function and add is the name of function.
Note-It is optional.
Function Definition: This is the place where actual code is written to perform the task. For example :

void add()
{
	int x,y=20,z=30;
	x=y+z;
	printf("Add=%d",x);
}

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 .For example :
add();
To call a function just write function name and put semi-colon(;) after it.

Complete Example

#include<stdio.h>
void add();//function declaration
void add()//function definition
{
	int x,y=20,z=30;
	x=y+z;
	printf("Add=%d",x);
}
int main()
{
	add();//function calling
}
/*
###Output###
Add=50
*/

Types of Function

There are two types of function
1.Pre-defined Function
2.User-defined Function

Predefined Function
The function which is predefined in the library is called predefined function. for example:-
printf(),scanf(),clrscr(),getch() etc.

User-defined Function

The function which is made by the user is called user-defined function. for example:-
add(),sub(),multi(),div()[Note: These are user-defined name.it may different.] etc.

Category of User-defined Function

There are four category 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

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>
void add();//function declaration
void add()//function definition
{
	int x,y=20,z=30;
	x=y+z;
	printf("Add=%d",x);
}
int 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.

#include<stdio.h>
void add(int y,int z);//function declaration
void add(int y,int z)//function definition
{
	int x;
	x=y+z;
	printf("Add=%d",x);
}
int main()
{
	//here 20 will assign to y and 30 to z
	add(20,30);//function calling
}
/*
###Output###
Add=50
*/

In the above example there are two parameter of integer type namely y and z 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>
int add();//function declaration
int add()//function definition
{
	int x,y=20,z=30;
	x=y+z;
	//return value of x
    return x;
}
int main()
{
	//assigning return value to variable rs
	int rs=add();//function calling
	printf("Add=%d",rs);
}
/*
###Output###
Add=50
*/

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

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>
int add(int y,int z);//function declaration
int add(int y,int z)//function definition
{
	int x;
	x=y+z;
	//return value of x
    return x;
}
int main()
{
	//assigning return value to variable rs
	int rs=add(20,30);//function calling
	printf("Add=%d",rs);
}
/*
###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 rs.

Calling of Function

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

Call by Value

In this 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.

#include<stdio.h>
 void add(int m)//function definition
{
	//adding 10 to m
	m=m+10;
}
int main()
{
	int x=10;
	printf("Before Calling x=%d\n",x);
	add(x);
    printf("After Calling x=%d",x);
}
/*
###Output###
Before Calling x=10
After Calling x=10
*/

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

Call by Reference

1.In this type of calling a function ,the reference of the value is passed at the time of calling.
2.In call by value the changes made in formal parameters reflect in actual parameters.
3.Reference is also called address.
4.When the address of data is passed at the time of calling so it is necessary to use pointer in the place of parameter.
For better understanding see the example below-

#include<stdio.h>
 void add(int *m)//function definition
{
	//adding 10 to m
	*m=*m+10;
}
int main()
{
	int x=10;
	printf("Before Calling x=%d\n",x);
	add(&x);
    printf("After Calling x=%d",x);
}
/*
###Output###
Before Calling x=10
After Calling x=20
*/

To know the difference between call by value and call by reference focus on the output of the program.

Function with default value

In this type of function ,the function contains a number of parameter with some initial value[for example: void sum(int x=10,int y=20)].At the time of calling if there is no value is passed [for example: sum();] then the default value will be x=10 and y=20,but if value is passed [for example: sum(5,6);] then the value will be x=5 and y=6.
For better understanding see the example below-

#include<stdio.h>
void sum(int x=10,int y=20)//function definition
{
	int result=x + y;
	printf("Add=%d\n",result);
}
int main()
{
	int x=10,y=20;
	printf("Without value\n");
	sum();//function calling without value
	printf("With value\n");
	sum(5,6);//function calling with value
}
/*
### Output ###
Without value
Add=30
With value
Add=11
*/

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 sum(int ar[5])//function definition
{
	int s=0;
	for(int i=0;i<5;i++)
	//finding the sum
	s=s+ar[i];
	printf("Total sum of element=%d",s);
}
int main()
{
	int x[5]={10,20,50,40,60};
	sum(x);//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]={10,20,50,40,60} and it is passed at the time of calling therefor the value of array x will be copied into array ar.

Recursion

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

Factorial of any Number using recursion
Factorial of 5=5*4*3*2*1
#include<stdio.h>
void factorial(int no,int f)//function definition
{
	if(no>=1)
	{
	 f=f*no;
	 no--;
	 factorial(no,f);
    }
    else
    printf("Factorial =%d",f);
}
int main()
{
	int n;
	printf("Enter any number to find factorial\n");
	scanf("%d",&n);
	factorial(n,1);//function calling with array
}
/*
### Output ###
Enter any number to find factorial
6
Factorial =720
*/
Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......

Post a Comment

0 Comments