The function in c language tutorial, we will teach you how to declare a function, define function and call function in c programming.
Suppose you are creating an application, and you need to repeat some code regularly. Instead of writing,
What is a Function ?
- Function is a group of code known as module.
- Function performs a specific task (computation).
- It can take information as a parameter.
- It can return information.
Types of Function
- User Define Function
- Library Function
User Defined Function in C
The Syntax of Function in C.
return_type function_name(arguments)
{
//function body to be executed
}
Example 1: C program, to declare, define and call function.
#include<stdio.h>
void fun(); //function declare
int main()
{
fun(); //function calling
}
//function definition
void fun()
{
printf("Learn from Coder Mantra");
}
- Declaration : It is also know as function prototype, it tells the compiler about function name, return type and parameters .
- Definition : Here you define actual body of function or block of code to perform a task.
- Calling : Using a function in your program is called calling function.
Types of User Defined Function in C.
1. Function without parameter and not returning a value
//void as a return type means that the function does not return any value, it only performs a task
#include<stdio.h>
void square()
{
int n;
printf("\nEnter a number : ");
scanf("%d",&n);
printf("\nSquare=%d",n*n);
}
int main()
{
square();
return 0;
}
Sample Output:Enter a number : 9
Square=81
Definition can be used in the place of declaration in C language.
2. Function with parameter and not returning a value
//Function accept an integer value as an argument, so whenever you call this function, you need to pass a whole number or integer variable as a parameter.
#include<stdio.h>
void test(int n)
{
if(n>0)
printf("\nPositive");
else if(n<0)
printf("\nNegative");
else
printf("\nZero");
}
int main()
{
int num;
printf("\nEnter a number : ");
scanf("%d",&num);
test(num);
return 0;
}
Sample Output:Enter a number : -65
Negative
3. Function with parameter and returning a value
//Function accept a double value as an argument, and return a double value, further you can use that value in your code.
#include<stdio.h>
double sqr(double r)
{
return r*r;
}
int main()
{
double radius,area;
printf("\nEnter Radius of Circle : ");
scanf("%lf",&radius);
area=3.14*sqr(radius);
printf("\nArea : %lf",area);
return 0;
}
Sample OutputEnter Radius of Circle : 5
Area : 78.500000
- Formal Parameter: The parameter, that we declare at the time of function prototyping.
- Actual Parameter: The parameter, that we pass to function at the time of function call.
Call by Value in C.
When we pass the value of the actual parameter at the time of function call, then the value of actual is assigned to the formal parameter. This concept is known as call by value and by default, every function is called call by value in c.
#include<stdio.h>
void fun(int n)
{
n=n+5;
printf("\n%d",n);
}
int main()
{
int num=10;
printf("\n%d",num);
fun(num);
printf("\n%d",num);
return 0;
}
Sample Output:10
15
10
Call by Reference in C.
When we pass the address of the actual parameter to the formal parameter at the time of function calling is known as call by reference. Any changes of formal parameter withing function will directly reflect the
#include<stdio.h>
void fun(int *n)
{
*n=*n+5;
printf("\n%d",*n);
}
int main()
{
int num=10;
printf("\n%d",num);
fun(&num);
printf("\n%d",num);
return 0;
}
Sample Output:
10
15
15
Recursion (Recursive Function) in C.
When a function call itself, is known as recursion or recursive function in c language. It behaves like a loop, and repeatedly call itself.
// Recursive function function example, below program will print Hello Coder continuously, to stop press ctrl+c.
#include<stdio.h>
void fun()
{
printf("\nHello Coder");
fun(); //function call itself
}
int main()
{
fun();
return 0;
}
Sample OutputHello Coder
Hello Coder
Hello Coder
……….
//Example : recursive function to print factorial of given number.
#include<stdio.h>
int fact(int n)
{
int f;
if(n==1)
return 1;
else
{
f=fact(n-1)*n;
return f;
}
}
int main()
{
int n;
printf("\nEnter a number");
scanf("%d",&n);
printf("\n%d",fact(n));
return 0;
}
Sample OutputEnter a number 5
120