Using function return by reference to modify a global array – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#2754 siteicon   siteicon   siteicon  

Using function return by reference to modify a global array

A program that neatly modifies a global array by using the function return by reference technique.

Learning Objectives

  • Use of function returning reference in case of pointing to each array index, so that the values can be intuitively modified.

Source Code

TC++ #2754


Source Code

Run Output

11 21 31 41 51

Code Understanding

int& modifyga(int); This function takes an integer input and return the reference to the place where modified value of input is stored.

int ga[]={10,20,30,40,50};
This is global array declared and initialised before the main routine which will be modified.

for(int i=0;i<5;++i) //Loop to use the modification function many times.

Here left hand side is showing a function as it is a reference returning function. In each iteration the current value of global array ga will be modified to a new value which adds 1 to previous value.

for(int i=0;i<5;++i) cout<<ga[i]<<” “;
This is final array values printing loop

int& modifyga(int i) { return ga[i]; }
This function returns the reference to an array based on the index passed to it.


  • In this function returning reference provide a quite intuitive way of writing array modification with common rules.
  • The same method can used to make other kind of modifications, say multiplying each value by 2, squaring each value and many similar things.
  • Such functions should be written for global/static scope variables or arrays.

Suggested Filename(s): fnref-modify-array.cpp, frefarr.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