-->
Showing posts with label call by value. Show all posts
Showing posts with label call by value. Show all posts

NEB31:What is pointer?explain it. With the help of program, explain about 'call by value' and 'call by reference'.

ans:
first part:-
                A pointer is a variable which points to the address and not its value. with the help of pointer, we can
                       ->we can allocate memory dynamically
                       ->better memory management
                        ->passing of array and string to the functions more efficiently
                        ->it helps us to return more values etc.
example:
   int *k;
here , k is a pointer and will store address of a variable.

second part:-
call by value:-
                         We can pass argments/parameters either by value or address.
 passing by value means we are going to copy the value directly to the formal arguments from real arguments. the change is formal arguments have no effect in real argument.Example is here.
// call by value
#include <stdio.h>

void callbyvalue(int, int);                            /* function Prototype i.e. function declaration */

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

                                                                    /* actual arguments will be as it is */
  callbyalue(x, y);                             // passing value of x and y
  printf("x= %d, y= %d\n", x, y);    // displaying the values
}

void callbyvalue(int m, int n)             // passing values to formal parameter m and n
{
  int t;
  t = m;
m = n;
 n = t;                              // swapping takes place
}

out put is: x=10,y=20
Here, the values of x and y are no affected by  values of m and n. so there will be no swapping.

call by reference:- We have now second method to pass our arguments/paramenter.In this, we use pointer to pass addressof parametersto the functions. in this way the values are copied to formalparameter and process continues there.example is given here.


// call by value reference
#include <stdio.h>
void callbyref(int*, int*);                                                          /* function Prototype */

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

                                                                                                  /* actual arguments will be altered */
  callbyref(&x, &y);                                                               // passing addresss
  printf("x: %d, y: %d\n", x, y);                                            //displaying value after altering
}

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


output: x=20,y=10
In above example we have passed address of identifier x and y to the function.while passing address,its vale is copied to formal parameter and then swapping takes place. It means by passing address we can alter the value in real parameter which was not possible in call by value,