call by value, pointer, reference

 Call By Value, Pointer, References

Here in this post, we have discussed the different parameter which we are pass in our user-defined function. The above all parameters are used for different situations. Call by value, call by reference, and call by pointer are three basic parameters which we often use in our function.

Note :

  • If we don't change our value and only we want to change its duplicate value then we only pass the call by value parameter in our functions.
  • And if we want to change the original value of our data then we will pass by the pointer or pass by the references.
 

Call By ValueCall By Reference
  • While calling a function, we pass the copy of the values of variables to it. Such functions are known as “Call By Values”.
  • While calling a function, instead of passing the values of variables, we pass the address of variables(location of variables) to the function known as “Call By References.
  • In this method, the value of each variable in the calling function is copied into corresponding it's dummy variables of the called function.
  • In this method, the address of actual variables in the calling function is copied into the dummy variables of the called function.
  • With this method, the changes made to the dummy variables in the called function have no effect on the values of actual variables in the calling function.
  • With this method, using addresses we would have access to the actual variables and hence we would be able to manipulate them.
// call by value

#include 

// Function Prototype
void swapx(int x, int y);

// Main function
int main()
{
    int a = 10, b = 20;

    // Pass by Values
    swapx(a, b);
printf("Before Swap The Value");    
printf("\na=%d b=%d\n", a, b);

    return 0;
}

// Swap functions that swaps
// two values
void swapx(int x, int y)
{
    int t;

    t = x;
    x = y;
    y = t;
    printf("After Swap The Value");
    printf("\nx=%d y=%d\n", x, y);
}


Output:
Before Swap The Value
x=20 y=10
After Swap The Value
a=10 b=20
// Call by Reference

#include 

// Function Prototype
void swapx(int*, int*);

// Main function
int main()
{
    int a = 10, b = 20;

    // Pass reference
    swapx(&a, &b);
    printf("Before Swap The Value");
printf("a=%d b=%d\n", a, b); return 0; } // Function to swap two variables // by references void swapx(int* x, int* y) { int t; t = *x; *x = *y; *y = t;     printf("Before Swap The Value");
printf("x=%d y=%d\n", *x, *y); } Output:
Before Swap The Value
x=20 y=10
After Swap The Value
a=20 b=10
  • Thus actual values of a and b remain unchanged even after exchanging the values of x and y in the swap function.
  • Thus actual values of a and b get changed after exchanging values of x and y in the swap function.
  • In call by values, we cannot alter the values of actual variables through function calls and any other operation in dummy variables.
  • In call by reference we can alter the values of variables through function calls.
  • Values of variables are passes by the Simple technique.








  • Pointer variables are necessary to define to store the address values of variables.

Comments

Popular posts from this blog

Datatypes In C++

Implicit And Explicit Type Conversion

Operators in C++