• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Functions » C++ function Call by Reference
Next →
← Prev

C++ function Call by Reference

By Dinesh Thakur

In addition to call by value, a function can also be called by reference. C++ provides two ways of passing arguments as reference to a function. The arguments can be passed either as a reference type or as a pointer type.

A reference is an alias or an alternate name for a variable. When an argument is passed by reference, an alias or a reference of the actual argument is passed to the formal argument. Unlike call by value method, call by reference does not create a copy of the actual argument and the called function works with the original values. This implies that any changes made to the variable in the function body are reflected in the calling function.

In call by reference, a reference to an argument is created by suffixing the reference operator or ampersand (&) to the data type of an argument in both the function definition and the declaration. Note that if the reference operator is omitted in either the function definition or the function declaration, the compiler raises an error. However, reference operator is not specified in the function call.

The syntax of declaring a function with a reference type parameter is

return_type function_name (Type& identifier)

To understand the concept of call by reference, consider the function inc ( ) defined in

Example. If the arguments are passed by reference, the prototype of the function becomes

void inc(int&&, int&&);

The function definition of inc ( ) can be given as

void inc(int&&& a, int&&& b) {
   // function body is same as in Example 5
}

The output of the program is

Values before function call in main():

x == – 2 and y == 7

Values within function inc():

a== -1 and b == 8

Values after function call in main():

x== -1 and y == 8

In this example, a and b become aliases to the actual arguments x and y. The inc ()function then increments the formal arguments a and b, and the changes are reflected in the actual arguments x and y in main ().

The call by reference mechanism requires no overhead of creating copies of arguments in the function and hence, it is faster and reduces memory utilization. It is efficient to use call by reference mechanism when the argument occupies a lot of memory space and copying the argument is not feasible.

Arrays as Arguments: Since an array represents a complete block of memory, an entire array cannot be passed to a function. However, a reference to the array can be passed to the function to avoid copying a big size of data. This implies that the called function works with an original array, however, referring to it by a different name.

Though an array is passed as a reference, a reference operator or ampersand (&) is not used to denote a reference argument as the array name itself contains the address of the first element. The syntax of passing an array to a function is given here.

return_type function_name (type identifier []);

It can also be written as

return_type function_name(type[]);

The syntax of a function call passing an array as an argument is

function_name (array_name) ;

To understand the concept of an array as an argument, consider this example.

Example : A program to sort the elements using bubble sort

#include<iostream<
using namespace std;
int i, j, temp;
void BubbleAsc(int [], int);
int main () {
  const int max = 10;
  int arr[max] = {22,5,67,98,45,32,101,99,73,10};
  cout<<”The list before sorting is: \n";
  for (i = 0; i < max; ++i)
    cout<<arr[i]<<" ";
    BubbleAsc (arr, max);
    cout <<"\nThe sorted list in ascending order is: \n";
    for (i = 0; i < max; i++)
    cout<<arr[i]<<" ";
    return 0;
}
void BubbleAsc(int num[], int max) {
   for (i = 0; i < (max - 1); i++) {
     for (j = 1; j < max; j++) {
      if (num[j] < num[j-1]) {
          temp = num[j];
          num[j] = num[j-1];
          num[j-1] = temp;
}}}}

The output of the program is

The list before sorting is

22 5 67 98 45 32 101 99 73 10

The sorted list in ascending order is

5 10 22 32 45 67 73 98 99 101

In this example, a reference num to array arr of type int is passed to the function BubbleAsc ()and thus, the changes made to num in BubbleAsc ()are reflected (see output) in the array arr in main ( ).

In addition to a single dimensional array, a multi-dimensional array can also be passed to a function. The syntax of passing a multi-dimensional array to a function is return_type function_name (type[] [size] [size] ).;

To understand the concept of passing a two-dimensional array to a function, consider this example.

Example : A program to multiply matrix by a number

#include<iostream>
using namespace std;
int i, j;
void PrintMatrix(int[] [2]);
void Multiply(int[] [2] ,int);
int main() {
  int arr[2] [2] ={10, 43, 32, 64};
  int x;
  cout<<"\nThe original Matrix is:";
  PrintMatrix(arr) ;
  cout<<"\nEnter a number: ";
  cin>>x;
  Multiply (arr, x);
  cout<<"\nThe new Matrix is:";
  PrintMatrix(arr) ;
  return 0;
}
void ,PrintMatrix(int num[2] [2]) {
   for(i=0;i<2;i++) {
     cout<<endl;
   for(j=0;j<2;j++)
     cout<<num[i] [j]<<” “;
}}
void Multiply(int num[2] [2], int x) {
   for(i=0; i<2;i++) {
     for(j=0;j<2;j++) {
       num [i] [j] = num[i] [j]*x;
}
}

The output of the program is

The original Matrix is:

10 43

32    64

Enter a number: 5

The new Matrix iS:

50     215

160   320

In this example, the array arr is passed to function Multiply(). Then a call to printMatrix()is made, to print the elements of an array num on the screen. Note that since reference of the array is passed to the function, changes are made to the original values.

The const Reference: When an argument is passed by reference, the compiler accesses the argument from its original location. This speeds up the execution of the program but on the other hand, the called function can modify the value of the argument and hence, the original value would be lost. Thus, in order to have the efficiency of call by reference as well as to prevent the modification of the value of the passed argument, it can be passed as a constant reference. When doing this, the compiler accesses the argument from its original location and also ensures that the value of the argument stays intact.

The constant reference is declared by prefixing the const keyword to the data type of the argument in the function declaration and definition. For instance, the function definition of a function sq_area()defined in Example , accepting a const reference can be given as

int sq_area(const int& s) {
  s++;
  return s*s;
}

Structure as Reference Arguments: The structure reference is passed by suffixing an ampersand (&) to the name of the structure in both the function declaration and the definition. To understand the concept of structure as reference arguments, consider the prototype of the function calc () in

Example , which Can be given as

void calc (details&) ;

The function definition can be given as

void calc(details& dd)

II function body is the same as in Ex

The output of the program is

Total amount in called function 1500

Total amount in main(): 1500

In this example, the structure variable d1 of structure details is passed by reference to function calc (). So, the modified value of the data member total of structure details is reflected in main () .

You’ll also like:

  1. Function call by reference in C
  2. Difference Between Call by Value and Call By reference in Java
  3. Call by Value and Call by Reference
  4. Call by value and Call by reference in C
  5. C Program Interchange Values of 2 no: using Call By Reference
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW