Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Pass by Reference / Value in C++

Pass by Reference / Value in C++ | DSA in C++ - Software Development PDF Download

Introduction

In C++, when passing arguments to functions, you have two options: pass by value and pass by reference. Understanding the difference between these two methods is crucial in Data Structures and Algorithms (DSA) as it affects how data is handled and can have an impact on performance. In this article, we will explore the concepts of pass by reference and pass by value in C++ and provide examples to help you grasp the concepts easily.

Pass by Value

When passing arguments by value, the function receives a copy of the value, and any modifications made within the function do not affect the original value. Let's take a look at an example to illustrate this:

#include <iostream>

void changeValue(int num) {

    num = 10;

}

int main() {

    int number = 5;

    std::cout << "Before function call: " << number << std::endl;

    changeValue(number);

    std::cout << "After function call: " << number << std::endl;

    return 0;

}

Output:

Before function call: 5

After function call: 5

In the above code, we define a function 'changeValue' that takes an integer argument 'num' and sets its value to 10. In the 'main' function, we declare an integer variable 'number' and initialize it with a value of 5. We then call the 'changeValue' function, passing 'number' as an argument. However, the value of 'number' remains unchanged outside the function. This is because the function receives a copy of the 'number' value, and any modifications made within the function do not affect the original value.

Pass by Reference

On the other hand, when passing arguments by reference, the function receives a reference to the original value. Any modifications made within the function will affect the original value. Let's see an example to understand this better:

#include <iostream>

void changeValue(int& num) {

    num = 10;

}

int main() {

    int number = 5;

    std::cout << "Before function call: " << number << std::endl;

    changeValue(number);

    std::cout << "After function call: " << number << std::endl;

    return 0;

}

Output:

Before function call: 5

After function call: 10

In this example, we define the function 'changeValue' that takes an integer reference 'num'. Inside the function, we modify the value of 'num' to 10. When we call the function 'changeValue' in the 'main' function, passing 'number' as an argument, the value of 'number' is modified to 10. This is because we are passing the reference to the original value, allowing us to modify it directly.

Passing Arrays

The concept of pass by reference and pass by value also applies to arrays. Let's look at an example:

#include <iostream>

void changeArray(int arr[]) {

    arr[0] = 10;

}

int main() {

    int array[] = {1, 2, 3};

    std::cout << "Before function call: " << array[0] << std::endl;

    changeArray(array);

    std::cout << "After function call: " << array[0] << std::endl;

    return 0;

}

Output:

Before function call: 1

After function call: 10

In this example, we define a function 'changeArray' that takes an array 'arr' as an argument. Inside the function, we modify the value of the first element of the array to 10. When we call the function 'changeArray' in the 'main' function, passing 'array' as an argument, the first element of the array is modified to 10. This demonstrates how passing arrays by reference allows us to modify the original array.

Sample Problems

Problems 1: Write a C++ program to swap two numbers using pass by value and pass by reference.

#include <iostream>

void swapByValue(int a, int b) {

    int temp = a;

    a = b;

    b = temp;

}

void swapByReference(int& a, int& b) {

    int temp = a;

    a = b;

    b = temp;

}

int main() {

    int x = 5, y = 10;

    std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;

    swapByValue(x, y);

    std::cout << "After swap (pass by value): x = " << x << ", y = " << y << std::endl;

    x = 5;

    y = 10;

        std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;

    swapByReference(x, y);

    std::cout << "After swap (pass by reference): x = " << x << ", y = " << y << std::endl;

    return 0;

}

Output:

  • Before swap: x = 5, y = 10
  • After swap (pass by value): x = 5, y = 10
  • Before swap: x = 5, y = 10
  • After swap (pass by reference): x = 10, y = 5

In this problem, we define two functions, 'swapByValue' and 'swapByReference', to swap the values of two numbers. The 'swapByValue' function receives the numbers by value, while the swapByReference function receives them by reference. As you can see from the output, only the 'swapByReference' function successfully swaps the values, as it operates on the original variables directly.

Problems 2: Write a C++ program to find the maximum and minimum elements in an array using pass by reference.

#include <iostream>

void findMaxMin(int arr[], int size, int& max, int& min) {

    max = arr[0];

    min = arr[0];

    for (int i = 1; i < size; i++) {

        if (arr[i] > max) {

            max = arr[i];

        }

        if (arr[i] < min) {

            min = arr[i];

        }

    }

}

int main() {

    int array[] = {9, 5, 2, 8, 1};

    int size = sizeof(array) / sizeof(array[0]);

    int maximum, minimum;

    findMaxMin(array, size, maximum, minimum);

    std::cout << "Maximum element: " << maximum << std::endl;

    std::cout << "Minimum element: " << minimum << std::endl;

    return 0;

}

Output:

Maximum element: 9

Minimum element: 1

In this problem, we define the function 'findMaxMin' that takes an array 'arr', its size, and two integer references 'max' and 'min'. The function finds the maximum and minimum elements in the array by iterating through the elements and updating 'max' and 'min' accordingly. By passing max and min by reference, their values are modified within the function and can be accessed in the 'main' function.

Pass by reference and pass by value are important concepts in C++ as they impact how functions handle data. By understanding these concepts, you can effectively use them in DSA to improve performance and manipulate data efficiently.

The document Pass by Reference / Value in C++ | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Important questions

,

Objective type Questions

,

practice quizzes

,

Extra Questions

,

Free

,

mock tests for examination

,

past year papers

,

Previous Year Questions with Solutions

,

video lectures

,

Pass by Reference / Value in C++ | DSA in C++ - Software Development

,

Pass by Reference / Value in C++ | DSA in C++ - Software Development

,

study material

,

Sample Paper

,

pdf

,

ppt

,

Summary

,

shortcuts and tricks

,

MCQs

,

Pass by Reference / Value in C++ | DSA in C++ - Software Development

,

Exam

,

Semester Notes

,

Viva Questions

;