Image

C Language - Functions - Function

Function

A function is a self contained block which design and execute separately.

C support two types of function.
1 standard library function
2 user defined function
1 System define (standard library function)
It is predefined function in C.
For ex:-  sqrt(), power(),abs()….etc.
2 user define function
A function define by user for its own need and necessity then such type of function is called as user define function.
For ex:- void show()                       , int max(int a, int h)
              {                                                {
               //statement                              //statement
              }                                                }
Function definition
Function definition involved return type function name parameter list and function body.
Syntax
 function_name (parameterlist)
{
//function body
} 
Function prototype
Function prototype defines the function declaration or function signature.
 function_name (parameterlist)
EX: void show();
         int max (int a,int b);
 
// program to draw a line.
#include<stdio.h>
#include<conio.h>
void drawline(); //function declaretion
main()
 {
  drawline(); //function call
  getch();

 }
 void drawline()    //function defination
 {
  int i;
  clrscr();
  for(i=1;i<=80;i++)
  {
   printf("-");
  }
 }
o/p
--------------------------------------------------------------------

1 Call by value

Call by value means sending the values of the arguments to functions. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value or call by value.

EX: WAP for call by value
#include<conio.h>
#include<stdio.h>
#include
int swap(int,int);
main()
{
 int a,b;
 clrscr();
 printf("\nEnter Value of a and b ");
 scanf("%d%d",&a,&b);
 printf("\n Before Printing ");
 printf("\na = %d and b = %d",a,b);
 swap(a,b);
 getch();
}
int swap(int a,int b)
{
 int temp;
 temp=a;
 a=b;
 b=temp;
 printf("\n After Printing ");
 printf("\na = %d and b = %d",a,b);
}
o/p
Enter a value of a and b
2     3
Before printing
a=2  and b=3
After printing
a=3 and b=2

2 Call by reference

Call by reference means sending the addresses of the arguments to the called function. In this method the addresses of actual arguments in the calling function are copied into formal arguments of the called functions. Thus using these addresses we would have an access to the actual arguments and hence we would be able to manipulate them. Using a call by reference intelligently, it is possible to make a function return more than one value at a time, which involves the study of pointer.

EX: WAP for call by reference
#include<conio.h>
#include<stdio.h>
#include
int swap(int*,int*);
main()
{
 int a,b;
 clrscr();
 printf("\nEnter Value of a and b ");
 scanf("%d%d",&a,&b);
 printf("\n Before Printing ");
 printf("\na = %d and b = %d",a,b);
 swap(&a,&b);
 printf("\n After Printing ");
 printf("\na = %d and b = %d",a,b);
 getch();
}
int swap(int *q,int *p)
{
 int temp;
 temp=*q;
 *q=*p;
 *p=temp;
}
o/p
Enter a value of a and b
2     3
Before printing
a=2  and b=3
After printing
a=3 and b=2

Recursion function

A function which called itself repeatedly till condition is satisfied.

Syntax:
 function_name(parameterlist)
{
//statement
}
Function_name(argument_list) 
{
//statement
}
EX: WAP to find factorial by using recursion
#include<stdio.h>
#include<conio.h>
int factorial(int);
main()
{
 int fact,n;
 clrscr();
 printf("\nEnter any Number ");
 scanf("%d",&n);
 fact=factorial(n);
 printf("\n Factorial of %d is %d",n,fact);
 getch();
}
int factorial(int n)
{
 int f=1;
 if(n==1)
   return 1;
 else
   f=n*factorial(n-1);
 return f;
}
o/p
Enter any number 
5
Factorial of 5 is 120