Team 2550
Technical Documentation
 All Files Variables Pages
Argument Passing

Table of Contents

The data passed into a function is referred to as arguments. An argument is an actual value, but it can be attained from variables or even other function calls (assuming that it returns the correct data type).

Note
You have already used a few functions that accept arguments. setw() in iomanip accepts a single integer and cin.get() accepts several different argument configurations. pow() in cmath also accepts two arguments.

The argument list consists of a list of variable declarations, called parameters. The data is copied into the parameters, and you can refer to them within the function by name.

Pass by Value

In a pass by value parameter, the external data is copied into the function.

Example

#include <iostream>
using namespace std;
int add(int num1, int num2); //prototype, num1 and num2 are parameters
int main()
{
int x=1, y=2, z;
z = add(2, 3); // 2 and 3 are arguments
cout << z << endl;
z = add(x, y); // 1 & 2 (contents of x & y) are args
cout << z << endl;
z = add(1, y); // 1 and 2 (content of y) are args
cout << z << endl;
}
// Function implementation (header + body)
int add( int num1, int num2)
{
return num1 + num2;
}
Note
If you give a function too many arguments or the wrong data types (a float instead of an int, for example), the code will not compile.

Practice

  • Write a function named isEven() that:
    • accepts an integer as an argument
    • returns true if it's even
    • returns false if it's odd
  • Write a function named lowest that:
    • receives two integer values as arguments
    • returns: Whichever of the two arguments is lowest
    • Call it from main a few times with different values to verify it works in all cases

Pass by Reference

Passing by reference sends the address of a variable into a function instead of a copy of the variable. In order to pass a parameter by reference, place a & after the datatype of the parameter in the prototype and header. This small syntax detail can completely change the behavior of a program, so pay attention!

Examples

Passing by value, outputs

1 1

void funcA(int x);
int main() {
int a=1;
funcA(a); // passes contents of variable a (1)
cout << a << " ";
return 0;
}
void funcA(int x) {
cout << x << " ";
x++; //modifies local variable x
}

Passing by reference, outputs

1 2

void funcA(int& x); //& makes is "pass by ref"
int main() {
int a=1;
funcA(a); // passes reference to variable a
cout << a << " ";
return 0;
}
void funcA(int& x) {
cout << x << " ";
x++; //modifies variable a in main
}

When to use pass by reference

Pass by reference is used in two instances:

  1. When you want to get more than one piece of data out of a function. Pass by reference allows you to get data out of a function without it being "returned" via the return statement, which is limited to returning one item.
  2. When you're passing in a LARGE piece of data and don't want to have to copy it (which takes time and memory). For now, we focus on the first reason (getting more data out of function).
Note
The C language didn't have pass by reference, it used "pass by address", which is identical in concept to pass-by-reference but utilizes a different syntax. This is also still supported in C++ as it's a superset of C, so beware of code you find on the internet.

Practice

Write a function that takes two variables by reference and swaps their contents. Call it swapVars. Test it to make sure it works.