C++Introduction to C++Learn C++

Learn About Parameters Passed by Reference in Functions

In the functions that we created in previous posts, their parameters have always been passed by value. This means that, when we call a function, what is passed to the function are the values of these parameters on that moment of the call. Variables are copied into the variables represented by the function parameters. Let’s remember an example,

Here x and y values are copied to a and b parameters and returned with addition.

We can access to its parameters directly by using its parameters as references. In C++, references are indicated with an ampersand (&) following the parameter type, it points the address of the variable. So we can modify our function parameters as a reference parameters by using & character before their variable names.

Here we passed x and y by itself, and we didn’t do copy operation on a & b variables of our function. If a variable is passed by reference that means it is passed without a copy operation, just addressing the variable itself. The variable identified by the function parameter, becomes somehow associated with the argument passed to the function. This allows users to do modification on their corresponding local variables within the function are reflected in the variables passed as arguments in the call. Referencing is important to eliminate copy operations and speeds up functions.

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond

C++C++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?

Worth reading...
Learn to Use Parameters in Functions in C++