0% found this document useful (0 votes)
19 views

WWW W3schools Com CPP CPP - Function - Reference Asp

The document discusses passing parameters to functions by reference in C++. It provides an example of a function that swaps the values of two integer variables passed by reference. The example demonstrates how passing by reference allows changing the original argument values.

Uploaded by

Abenezer Asefa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

WWW W3schools Com CPP CPP - Function - Reference Asp

The document discusses passing parameters to functions by reference in C++. It provides an example of a function that swaps the values of two integer variables passed by reference. The example demonstrates how passing by reference allows changing the original argument values.

Uploaded by

Abenezer Asefa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

 Tutorials  Exercises  Services  

C#
 BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY

C++ Functions ‐ Pass By Reference


❮ Previous Next ❯

Pass By Reference
In the examples from the previous page, we used normal variables when we passed
parameters to a function. You can also pass a reference to the function. This can be
useful when you need to change the value of the arguments:

Example
void swapNums(int &x, int &y) {
int z = x;
x = y;
y = z;
}

int main() {
int firstNum = 10;
int secondNum = 20;

cout << "Before swap: " << "\n";


cout << firstNum << secondNum << "\n";

// Call the function, which will change the values of firstNum and
secondNum
swapNums(firstNum, secondNum);

cout << "After swap: " << "\n";


cout << firstNum << secondNum << "\n";
return 0;
}

Try it Yourself »

❮ Previous Next ❯

COLOR PICKER



SPACES

UPGRADE

NEWSLETTER

GET CERTIFIED

REPORT ERROR

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples

Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate

    FORUM ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve
reading and learning. Tutorials, references, and examples are constantly reviewed to avoid
errors, but we cannot warrant full correctness of all content. While using W3Schools, you
agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999­2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

You might also like