Function in C
Advantage of Function:
* Code Re-usability
* Develop an application in module format.
* Easily to debug the program.
Syntax of a Function:
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.return_type function_name( parameter list )
{
body of the function
}
Calling a function.:
When you call any function control goes to function body and execute entire code. For call any function just write name of function and if any parameter is required then pass parameter. At the time of function calling function must be terminated with ';'.
You can use function either with argument or without argument. If a function take any arguments, it must declare variables that accept the values as a arguments. These variables are called the formal parameters of the function.
function_name(); or variable=function_name(argument);
Example: Function without argument.
#include<stdio.h>
#include<conio.h>
void sum(); // declaring a function
clrscr();
int a=20,b=30, c;
void sum() // defining function
{
c=a+b;
printf("Sum: %d", c);
}
int main()
{
sum(); // calling function
getch();
return 0;
}
Output
Sum: 50
Example: Function with argument.
#include<stdio.h>
#include<conio.h>
void swap(int , int);
int main()
{
int m,n;
printf("\nEnter Two value:");
scanf("%d%d",&m,&n);
printf("\nValues before swap m=%d n=%d",m,n);
swap(m,n);
getch();
return 0;
}
void swap(int a,int b)
{
int c;
c=a;
a=b;
b=c;
printf("\nValues after swap m=%d n=%d",a,b);
}
There are two ways to pass value or data to function. By default, C uses call by value to pass arguments. Above example is call by value.
1. Call by value
2. Call by reference
Difference between call by value and call by reference.
Call by value | Call by reference |
---|---|
1) The argument is either variable or constant or an expression. | 1) The argument is address of variable. |
2) The parameter is variable which can store the argument. | 2) The parameter must be a pointer variable. |
3) The argument is copied within the parameter. Hence, arguments cannot be modified by the function. | 3) As the addres of variable is stored in the parameter. Hence the arguments can be changed by the function. |
Explain call by value and call by reference along with example.:
A function can be called by value or by reference. In call by value method the value of the arguments are copied into the formal parameters of the function. In this case changes made to the parameters have no effect on the arguments.
Example Call by value:
Difference between formal and actual argument.
The terms formal argument and actual argument are sometimes used for the same distinction." So don't get confused if you find the question like difference between actual and formal arguments or difference between actual and formal parameters. They both are the same. The major difference between actual and formal arguments is that actual arguments are the source of information; calling programs pass actual arguments to called functions. The called functions access the information using corresponding formal arguments.
Example:
#include<stdio.h>
#include<conio.h>
int addNo(int, int);
int main()
{
int n1 = 10, n2 = 20, sum;
sum = addNo(n1, n2);
printf("Sum of %d and %d is: %d \n", n1, n2, sum);
getch();
return 0;
}
int addNo(int a, int b)
{
return (a + b);
}
Recursive Function in C:
It is a programming technique that allows the programmer to express operations in terms of themselves. When Function is call within same function is called Recursion. The function which call same function is called recursive function. That means when a function call itself then that function is called Recursive function.
Syntax
void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}
Example: Find the Factorial of any number using recursion
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int i,f,num;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
f=fact(num);
printf("Factorial of %d = %d",num,f);
getch();
return 0;
}
int fact(int n)
{
if(n<0)
{
return (-1);
}
if(n==0)
{
return 1;
}
else
{
return(n*fact(n-1));
}
}
0 Comments