-->

program to understand call by value and call by reference

using codeblocks
--------------------------------------------------------------------------------
// call by value reference
#include <stdio.h>
void callbyref(int*, int*);             /* function Prototype with two pointer as parameter*/

int main()                            /* Main function */
{
  int x = 10, y = 20;                 //variable declaration

    printf("before calling\n");                         /* actual arguments will be altered */
  printf("x=%d,y=%d\n",x,y);        //value display before call
  callbyref(&x, &y);              // passing address
  printf("after calling\n");
  printf("x= %d, y= %d\n", x, y);   //displaying value after calling
  return 0;
}

void callbyref(int *m, int *n)
{
  int t;
  t = *m;
  *m = *n;
  *n = t;                       //swapping being done
}


----------------------------------------------------------------------------------------------
using turbo c++
-----------------------------------------------------------------------------------------
// call by value reference
#include <stdio.h>
#include<conio.h>
void callbyref(int*, int*);             /* function Prototype with two pointer as parameter*/

int main()                            /* Main function */
{
  int x = 10, y = 20;                 //variable declaration

    printf("before calling\n");                         /* actual arguments will be altered */
  printf("x=%d,y=%d\n",x,y);        //value display before call
  callbyref(&x, &y);              // passing address
  printf("after calling\n");
  printf("x= %d, y= %d\n", x, y);   //displaying value after calling
 getch();
  return 0;
}

void callbyref(int *m, int *n)
{
  int t;
  t = *m;
  *m = *n;
  *n = t;                       //swapping being done
}

No comments:

Post a Comment