Function Call by Reference and Call by Value comparison – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#2680 siteicon   siteicon   siteicon  

Function Call by Reference and Call by Value comparison

A program that uses both call by reference and call by value in the same program to compare it for better understanding.

Learning Objectives

  • Understanding call by reference and call by value, vis-a-vis each other with similar functionality but different approach.

Source Code

TC++ #2680

 

Source Code

Run Output

Before referenced function call: a = 10
After referenced function call: a = 11
Using call by value: a = 11
Using call by value: b = 12

Code Understanding

void increment(int &);
This is function prototype for call by reference. & denotes that the address of given variable location is to be passed. When the address is passed, the original value can be changed in the function also.

int increase_and_return(int);
This is function prototype of normal type call which is called the call by value call. Here the formal parameter in the function is passed and the variable scope of parameter is within the function only. The original value from where it is passed can not be changed by the called function.

int a=10; cout<<“Before referenced function call: a = “<<a<<endl;
Variable is set and displayed so that original value can be seen.

increment(a);
The function is called which is a call by reference function. Once it is called the value of a can be adjusted by the called function. The called function in this program is increasing the value passed.

cout<<“After referenced function call: a = “<<a<<endl;
This will print the incremented value. This should be 11 in this example.

int b=increase_and_return(a);
This function is also increasing the value by 1 but it is not changing the original value. The original value has become 11 in due to previous change in a so the returned value to b would be 12 but a will remain 11.

cout<<“Using call by value: a = “<<a<<endl;
cout<<“Using call by value: b = “<<b<<endl;
Both a and b as discussed above will be printed.

void increment(int &x) {++x;}
This is call by reference where the address location of x is being passed. This function modifies the original value as it has access to the original location.

int increase_and_return(int x){return ++x;}
This is call by value. It changes the value of x within its own scope and is not able to change the original value as it doesn’t have access to its address location. In turn it returns the modified value.

 

Notes

  • Call by reference gives advantage of making impact of functions on multiple variables at the same time but it has the disadvantage of accidentally and unknowingly change the values in the called functions.
  • Call by value can return just one value at a time, but it makes the operation fool proof as called function can not modify the original values even accidentally.

Common Errors

  • Student sometime misunderstand the & sign. If they remember it as address of (&) operator they will understand that reference actually means location in the memory.


Suggested Filename(s): cbr-cbv.cpp



Share

sunmitra| Created: 16-Jan-2018 | Updated: 16-Jan-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back