2/25/2009

Different Methods Of Calling Functions In C Programming Language

Methods of calling funcctions in C:
===================================
 1) Call by value:
 =================
    In call by value, only the value will be passed.If any modification is done for the formal parameters within a function it does not reflect on the actual parameters.

 2) Call by reference:
 =====================
    In pass by reference both, value and adddress of the variable will be passed. If any modification is done by a formal parameters within a function, it reflects on actual parameters.

 3) Call by value_Result
 =======================
 
 4) Call by result
 =================

 5) Call by constatnt value
 ==========================
    

/* 1. Call by value*/
#include <stdio.h>
 
/* function declaration */
void swap(int x, int y);
/* function definition to swap the values */
void swap(int x, int y)
{
   int temp;

   temp = x; /* save the value of x */
   x = y;    /* put y into x */
   y = temp; /* put temp into y */
  
   return;
}
 
int main ()
{
   /* local variable definition */
   int a = 100;
   int b = 200;
 
   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );
 
   /* calling a function to swap the values */
   swap(a, b);
 
   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );
   printf("Did not swap. ");
 
   return 0;
}

/* 2. Call by reference*/
#include <stdio.h>
#include <conio.h>


/* function definition to swap the values */
void swap(int *a, int *b)
{
   int temp;

   temp = *a; /* save the value of x */
   *a = *b;    /* put y into x */
   *b = temp; /* put temp into y */

   return;
}

int main ()
{
   /* local variable definition */
   int a = 100;
   int b = 200;

   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );

   /* calling a function to swap the values */
   swap(&a, &b);

   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );

   return 0;
}

/* 3. Call by value_result*/

#include <stdio.h>
#include <conio.h>

/* function declaration */
int area(int l, int w);
 
/* function definition to calculate the area */
int area(int l, int w)
{
   return(l*w);
}
 

int main ()
{
   /* local variable definition */
   int l = 12;
   int w = 5;
 
   printf("area : %d\n", area(l,w) );
   
   return 0;
}

/* 4. Call by result*/

#include <stdio.h>
#include <conio.h>

int a=3,b=5;
/* function definition to calculate the sum */
int sum()
{
   return(a+b);
}
 

int main ()
{
   /* local variable definition */
   int result;
   
   result=sum();

   printf("Sum : %d\n", result );
  
   return 0;
}

/* 5. Call by constatnt value*/

#include <stdio.h>
#include <conio.h>

/* function definition to calculate the area */
float area(const float pi, float r)
{
   return(pi*r*r);
}
 

int main ()
{
   /* local variable definition */
   const float pi=3.14;
   float r=2.5;
 
   printf("Area : %.2f\n", area(pi,r) );
  
   return 0;
}

No comments:

Post a Comment