91-9990449935 0120-4256464 |
Call by value and call by reference in CThere are two ways to pass value or data to function in C language: call by value and call by reference. Original value is not modified in call by value but it is modified in call by reference. Let's understand call by value and call by reference in c language one by one. Call by value in CIn call by value, original value is not modified. In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main(). Let's try to understand the concept of call by value in c language by the example given below: OutputBefore function call x=100 Before adding value inside function num=100 After adding value inside function num=200 After function call x=100 Call by reference in CIn call by reference, original value is modified because we pass reference (address). Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function. Note: To understand the call by reference, you must have the basic knowledge of pointers. Let's try to understand the concept of call by reference in c language by the example given below: OutputBefore function call x=100 Before adding value inside function num=100 After adding value inside function num=200 After function call x=200 Difference between call by value and call by reference in c
Next Topicrecursion in C
|