Function returning by reference – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#2749 siteicon   siteicon   siteicon  

Function returning by reference

A program to demonstrate returning of a reference data type from the function.

Learning Objectives

  • Use of function which can return location of returned variable rather than the value of it.

Source Code

TC++ #2749


Source Code

Run Output

Global var g before change = 10
Global var g after function call = 20

Code Understanding

int& setg();
This function prototype with & marked immediately after the return data type. This means that functions instead of returning a value is actually returning a reference to a location where the value is stored.

int g=10; //This global declaration of variable g with initial value set as 10.

cout<<“Global var g before change = “<<g<<endl; //The initial value is printed here

This would look different to you as the function call is on the left hand side of assignment. This perfectly okay as left side is supposed to be an address to an location. This function is actually a reference returning function so it is actually a place where something is stored. So whatever place it is pointing to it will store 20 after this assignment is made.

cout<<“Global var g after function call = “<<g<<endl;
The newly changed value will be printed here.

int& setg() { return g; }
This function simply returns the location pointing to g. This location must be a global or static location so that it can become available to other routines.


  • There are many pitfalls in using functions returning references, however there are situations where this may be useful or useful.
  • Modern programmers use pointers or smart pointers instead of returning by & reference. Pointers are also references with more intuitive tricks for references.

Common Errors

  • Putting & at the wrong place and trying to return non-static or constants as references are common mistakes people do.

Suggested Filename(s): fn-ref-return.cpp, ref-ret.cpp


sunmitra| Created: 18-Jan-2018 | Updated: 18-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