Call by value in C

Call by value passes arguments to a function, and it copies the actual value of an idea into a formal parameter of a function. Changes made to a formal parameter inside the function do not affect the argument. That is, operations performed on formal parameters do not reflect the actual parameters. In other words, code within a function cannot alter the arguments used to call the function. By default, in the C programming language, the program uses “call by value” to pass arguments.

It is a convention where a caller checks the actual parameters and passes their values to a callee. Modifications of the value done by the callee will not be visible to the caller.

Eg:

Terminologies used in the call by value method:

  • Actual parameters – parameters that appear in function calls.
  • Formal parameters – parameters that appear in the function declaration.

Call by value occurs when you pass the actual parameter while calling a function.

Eg:

Output:

In the above example, we have passed variable n1 while calling the method, and we are also calling a function using the call by value method. Still, only the value of n1 is copied to the formal parameter. Hence changes made to the function “increment” do not affect the variable n1.

Functions in C language can be invoked in two different ways:

  1. Call by value: as explained, values of actual parameters are copied to the function’s formal parameter, and these two different types of parameters are stored in two separate memory locations.
  2. Call by reference: in contrast to the “call by value” method, both actual and formal parameters refer to the exact memory locations; hence any changes made in the function will reflect the actual parameter, that is, operations done on formal parameter inside a function will lead to changes of the actual parameter.

Advantages of using the ‘Call by Value’ method:

  • The method does not change the original variable, so it will preserve the original data that will be declared.
  • Every time a function is called, it will never affect the actual data values of the arguments.
  • The value of an actual argument passed is passed on to the formal argument, so any changes that are made in a formal argument do not affect the real values.

Disadvantages of using the ‘Call by Value’ method:

  • Changes in actual parameters modify corresponding changes in argument functions.
  • Arguments must be variables in the call by value method.
  • You cannot change the variable in the function directly, and you can only change it by changing the actual parameter’s value.
  • Two copies will be created for the same variable, which will not be memory efficient.

Difference between Call by value and Call by reference:

Call by ValueCall by Reference
When calling a function, you have to pass values by copying variables to other arguments.In this method, instead of copying the values of the variables, the address of the variables is used in place of actual values.
Copy of variables is passed in call by valueVariable itself is passed in call by reference
Alteration of variables is not permitted in call by value.Allows changes in the values of variables by using function calls.
Values of the variables are passed using function methodsPointer variables are necessary to store the address of variables.
Actual and formal arguments will be created in different memory locationsActual and formal arguments will be created in the same memory location
Actual arguments remain safe as they cannot be modified accidentallyActual arguments are not safe, and can be accidentally modified; hence should be handled carefully.

Pin It on Pinterest

Share This