-3

Could someone please explain the difference between parameter passing in C please? According to professor notes there are 4 different ways to pass parameters

  1. Call-by-value
  2. Call-by-address (pointer)
  3. Call-by-alias
  4. Global variable / Static variable

If you could please give an example, I would greatly appreciate that, and your work would be commended.

  • 1
    C does not have 'call by alias' (whatever that is supposed to mean). C++ has references (maybe this is what was meant?), but then C++ is not C. – user2864740 Mar 17 '15 at 00:01

2 Answers2

4
  1. Call-by-value

    Passing the value to a function as a parameter. If the function modifies the variable, the actual variable won't get changed.

    void fun1(int myParam)
    {
        myParam = 4;
    }
    
    void main()
    {
        int myValue = 2;
        fun1(myValue);
        printf("myValue = %d",myValue);
    }
    

    myValue will always be 2.

  2. Call-by-address (pointer)

    void fun1(int *myParam)
    {
        *myParam = 4;
    }
    void main()
    {
        int myValue = 2;
        fun1(&myValue);
        printf("myValue = %d",myValue);
    }
    

    Here we are passing the address of myValue to fun1. So the value of myValue will be 4 at the end of main().

  3. Call-by-alias

    There is no alias in C as per my understanding. It should be the C++ reference mechanism.

  4. Global variable / Static variable

    Global and static variables are variables stored in a common places, accessible by the caller and callee functions. So both caller and callee will be able to access and modify them.

    int myValue = 2;
    void fun1()
    {
        myValue = 4;
    }
    void main()
    {
        myValue = 2
        fun1();
        printf("myValue = %d",myValue);
    }
    

    As you can guess, the value of myValue will be 4 at the end of main().

Hope it helps.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
George
  • 1,330
  • 1
  • 9
  • 12
  • Wow thank you so much. This makes much more sense than how it is taught in our program. Passing by address I can already tell is a very useful and nimble feature. I can also see why Java doesn't contain it due to compilation errors. I sincerely appreciate this response. – Frank Palmisano Mar 17 '15 at 00:22
  • @FrankPalmisano Java has passing by reference , in fact that is the only option for non-primitive types – M.M Mar 17 '15 at 00:53
2

C passes all function parameters by value, period; the formal parameter (in the definition) is a separate object in memory from the actual parameter (in the call). Any updates to the formal parameter have no effect on the actual parameter. You can fake pass-by-reference semantics by using a pointer, but the pointer is passed by value.

True pass-by-reference means that the formal and actual parameters refer to the same object in memory, so any changes to the formal parameter also affect the actual parameter. In practice, a pointer-like object is passed to the subroutine, but that's hidden from the programmer.

C does not support pass-by-reference. C++ supports pass-by-reference with special operators. Old-school Fortran was pass-by-reference.

Global variables are simply visible to both the caller and callee.

Can't speak to pass-by-name or pass-by-alias; never worked with a language that used that mechanism.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • @FrankPalmisano - [This Wikipedia entry](http://en.wikipedia.org/wiki/Evaluation_strategy) should help answer some of your questions as well. – John Bode Mar 17 '15 at 15:49