Thread: What happen exactly when we pass the value to function

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    113

    What happen exactly when we pass the value to function

    I wrote to understand basic of pass by value
    Code:
    #include<stdio.h> 
    /* function declaration */
    void pass(int x, int y);
    void pass(int x, int y)
    {
    	printf ( "\n x : %d", x);
    	printf ( "\n y : %d", y);	
    }
     
    int main () {
    
    
       /* local variable definition */
       int a = 100;
       int b = 200;
     
       printf("Before pass, value of a : %d\n", a );
       printf("Before pass, value of b : %d\n", b );
     
       /* calling a function to pass the values */
       pass(a, b);
     
       printf("\n After pass, value of a : %d\n", a );
       printf("\n After pass, value of b : %d\n", b );
     
       return 0;
    }
    Before pass, value of a : 100
    Before pass, value of b : 200


    x : 100
    y : 200
    After pass, value of a : 100


    After pass, value of b : 200

    I am not sure but i think when we call function value of x and y will replace with a and b

    we are passing value 100 and 200 to the function

    Is it the whole process of pass by value ?

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    240
    Each time you call the 'pass' function, two variables called x and y with static scope are created. As you pass the value of 'a' to 'x' and 'b' to 'y', you essentially end up creating two aliases aka duplicates of a and b. This is essentially all about pass by value. If I were to clone you and experiment on your cloned version, it is obvious that it'll have no effect on the 'real' you. Same with creating copies of variables in a pass by value situation.

    Passing by Value vs. by Reference Visual Explanation with animations and diagrams

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    113
    thank you so much

    what exactly happen call by reference

    Code:
    #include<stdio.h> 
    /* function declaration */
    void passbyrefrence(int *x, int *y);
    void passbyrefrence(int *x, int *y)
    {
       printf("\n After pass, value of x : %d\n", *x );
       printf("\n After pass, value of y : %d\n", *y );
       
       printf("\n After pass, address of x : %p \n", x );
       printf("\n After pass, address of y : %p \n", y );
    
    
       printf ("\n");
    }
     
     
    int main () {
    
    
       /* local variable definition */
       int a = 100;
       int b = 200;
     
       printf("Before pass, value of a : %d\n", a );
       printf("Before pass, value of b : %d\n", b );
     
       printf("Before pass, address of a : %p \n", &a );
       printf("Before pass, address  of b : %p \n", &b );
    
    
       passbyrefrence(&a, &b);
     
       printf("After pass, value of a : %d\n", a );
       printf("After pass, value of b : %d\n", b );
       
       printf("after  pass, address of a : %p \n", &a );
       printf("after pass, address  of b : %p \n", &b );
     
       return 0;
    }
    Before pass, value of a : 100
    Before pass, value of b : 200
    Before pass, address of a : 0061FF2C
    Before pass, address of b : 0061FF28


    After pass, value of x : 100


    After pass, value of y : 200


    After pass, address of x : 0061FF2C


    After pass, address of y : 0061FF28


    After pass, value of a : 100
    After pass, value of b : 200
    after pass, address of a : 0061FF2C
    after pass, address of b : 0061FF28

    definition say we pass parameter by reference

    Code:
     passbyrefrence(&a, &b);
    What is meaning of this line ?

    does it pass the address of a and b or it pass the content of a and b to x and b at the address a and address of b
    Last edited by abhi143; 11-15-2019 at 11:23 AM.

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    does it pass the address of a and b or it pass the content of a and b to x and b at the address a and address of b
    The former – it passes the address, not the content.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    240
    What is meaning of this line ?

    does it pass the address of a and b or it pass the content of a and b to x and b at the address a and address of b
    & is the 'reference' operator. Your function expects integer pointers. Now, if you have learnt what pointers are, then you'd be correct if you thought that they are just a variable that hold a memory address. Put the dots together and you have your answer. You pass the address of 'a' to 'x' which expects an address and the same with 'b' to 'y'.

    Take a look at the website I shared above, it'll clear all your doubts or misconceptions about pass by value and pass by reference. The take from here is, pass by value doesn't carry back the changes you do to the clone, but pass by reference affects you TOO if any change is done on your clone.

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    113
    Quote Originally Posted by -Adrian View Post
    The former – it passes the address, not the content.

    All arguments are passed by value in c language. ?
    or
    arguments get passed by value and/or reference in c language?
    Last edited by abhi143; 11-15-2019 at 12:03 PM.

  7. #7
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    If you insist on discussing semantics, you won't get a universal answer.
    Code:
    void foo(int a); // passing an integer
    void foo(int* a); // passing a pointer to an integer
    You know these two, you know how they work/differ, time to move on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-30-2012, 07:18 AM
  2. how to pass a set to function
    By iHateSicsic in forum C++ Programming
    Replies: 1
    Last Post: 01-02-2011, 10:24 AM
  3. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  4. C function - Pass by value? Pass By Ref?
    By stevong in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 08:02 AM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread