0% found this document useful (0 votes)
40 views3 pages

Function Calling in C: Value vs Reference

1) When a function is called, program control transfers to the called function, which performs its defined task before returning control back to the main program. 2) Functions can be called by passing required parameters and any return value. This is done through the function's name and parameters in the function call statement. 3) There are two ways functions can be called: call by value and call by reference. Call by value passes the value of an argument, while call by reference passes the address of the argument, allowing the function to modify the original value.

Uploaded by

Vin Diesel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Function Calling in C: Value vs Reference

1) When a function is called, program control transfers to the called function, which performs its defined task before returning control back to the main program. 2) Functions can be called by passing required parameters and any return value. This is done through the function's name and parameters in the function call statement. 3) There are two ways functions can be called: call by value and call by reference. Call by value passes the value of an argument, while call by reference passes the address of the argument, allowing the function to modify the original value.

Uploaded by

Vin Diesel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Function Calling in C (call by value and call by reference)

Calling a function
When a program calls a function, the program control is transferred to the called function. A
called function performs a defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns the program control back to the main
program.

To call a function, you simply need to pass the required parameters along with the function
name, and if the function returns a value, then you can store the returned value.

Control of the program is transferred to the user-defined function by calling it.

Syntax of function call


functionName(argument1, argument2, ...);

In the above example, the function call is made using addNumbers(n1, n2); statement inside the
main() function.

Call by Value and Call by Reference


Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways
are generally differentiated by the type of values passed to them as parameters.

The parameters passed to function are called actual parameters whereas the parameters received
by function are called formal parameters.

Call By Value in C: In this parameter passing method, values of actual parameters are copied to
function’s formal parameters and the two types of parameters are stored in different memory
locations. So any changes made inside functions are not reflected in actual parameters of caller.

In other words, in this parameter passing method, values of actual parameters are copied to
function's formal parameters, and the parameters are stored in different memory locations. So
any changes made inside functions are not reflected in actual parameters of the caller.

Call by Value Example: Swapping the values of the two variables

1. #include <stdio.h>
2. void swap(int , int); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %d\n",a,b);
8. swap(a,b);
9. printf("After swapping values in main a = %d, b = %d\n",a,b); }
By: Pragya Singh Tomar
Page 1 ICS,Vikram University, Ujjain
Function Calling in C (call by value and call by reference)

10. void swap (int a, int b)


11. {
12. int temp;
13. temp = a;
14. a=b;
15. b=temp;
16. printf("After swapping values in function a = %d, b = %d\n",a,b);
17. }

Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

Call by reference in C

Call by reference method copies the address of an argument into the formal parameter. In this
method, the address is used to access the actual argument used in the function call. It means that
changes made in the parameter alter the passing argument.

In this method, the memory allocation is the same as the actual parameters. All the operations in
the function are performed on the value stored at the address of the actual parameter, and the
modified value will be stored at the same address. Means, both the actual and formal parameters
refer to same locations, so any changes made inside the function are actually reflected in actual
parameters of caller.

Call by reference Example: Swapping the values of the two variables

1. #include <stdio.h>
2. void swap(int *, int *); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %d\n",a,b);
8. swap(&a,&b);
9. printf("After swapping values in main a = %d, b = %d\n",a,b);
10. }
11. void swap (int *a, int *b)
12. {
13. int temp;
14. temp = *a;
15. *a=*b;
16. *b=temp;
17. printf("After swapping values in function a = %d, b = %d\n",*a,*b);
18. }

By: Pragya Singh Tomar


Page 2 ICS,Vikram University, Ujjain
Function Calling in C (call by value and call by reference)

Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

Call By Value Call By Reference


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

Actual and formal arguments are created at Actual and formal arguments are created at the
the different memory location same memory location

By: Pragya Singh Tomar


Page 3 ICS,Vikram University, Ujjain

Common questions

Powered by AI

A programmer might choose 'call by value' over 'call by reference' when they want to ensure that the actual parameters remain unchanged after a function call. This approach can ensure data integrity and prevent unintended side effects, as modifications made within the function do not affect the original values outside of it . It is also simpler in terms of memory management because it does not involve direct access to memory addresses, reducing the chance of pointer-related errors .

The primary distinction between 'call by value' and 'call by reference' lies in how parameters are passed to a function. In 'call by value', the values of actual parameters are copied to the formal parameters of the function; thus, changes made within the function do not affect the actual parameters. Each set of parameters occupies a different memory location . Conversely, 'call by reference' involves passing the address of the actual parameters, so the function manipulates the actual memory addresses. As a result, changes in the function affect the actual parameters directly because both sets of parameters refer to the same memory location .

In 'call by value', the memory allocation for formal and actual parameters is separate, meaning any modification to the formal parameters will not impact the actual parameters because they reside at different memory addresses . In 'call by reference', both formal and actual parameters share the same memory address, which allows modifications to the formal parameters to directly alter the actual parameters' values, as both refer to the same memory location .

In the 'call by value' example, swapping two variables inside a function has no effect on the original variables in the main function, as demonstrated by the unchanged values after the swap function is called . Contrastingly, in the 'call by reference' example, swapping actually alters the variables' values within the main function, showing the direct memory access effect when addresses are passed .

Understanding 'call by value' and 'call by reference' helps in debugging by clarifying how data is manipulated inside functions. By knowing whether a function will modify the actual arguments ('call by reference') or merely use their values ('call by value'), a programmer can better predict and track changes, identifying where and why unexpected modifications occur. This insight is crucial for tracing errors related to incorrect data handling or unexpected variable changes .

Swapping variables using 'call by reference' highlights pointers' role in managing memory and accessing variable values directly. Pointers enable functions to alter the actual variables by referencing their memory locations, showcasing pointers as fundamental in direct data manipulation. It underscores how pointers allow the construction of flexible and efficient functions that can affect variables beyond their local scope .

Drawbacks of using 'call by reference' include the risk of unintentional side effects where functions may inadvertently alter the caller's variables, leading to bugs or unexpected behavior . It also adds complexity due to the required use of pointers and can introduce errors if pointers are not managed correctly, such as issues with invalid memory access or memory leaks.

'Call by value' might be preferred in scenarios where data immutability is crucial, as it prevents a function from altering the caller's variables, ensuring that any changes are isolated. This approach can be advantageous when passing simple, small variables where the overhead of copying does not significantly impact performance, providing greater function predictability and reducing the complexity associated with pointer manipulation .

Using 'call by reference' allows a function to modify the original variable's value outside the function, as the function directly accesses the memory address of the variable. This can be beneficial when changes made inside a function need to be reflected in the outer scope, but it also increases the risk of unintentional side effects, since the function can alter variables in the calling scope without creating a copy .

Passing by reference affects function design by permitting the function to modify the caller's data directly, offering flexibility and efficiency when large data structures are manipulated, thus potentially reducing copies. However, it requires careful implementation to avoid errors like unintentional data modification. It necessitates pointer usage, which can elevate complexity and potential bugs if not managed correctly .

You might also like