Parameters and Arguments

Chris Wild & Steven Zeil

Last modified: Jun 11, 2020
Contents:

1 Calling Functions

1.1 Description

When you write a function, you do not know the name of the variables that will be the function’s input and output parameters. Yet in the function, you need some name so that you can write the code.

1.2 Example

void myFunction(int anInteger, float aFloat, char aChar)
// anInteger, aFLoat, aChar are the aliases (parameters) for the eventual 
// arguments
{
    //. . .
}
int main( )
{
    int  thisInt = 3; char thisChar = 'a'; float thisFloat = 3.4;
    myFunction(thisInt, thisFloat, thisChar);
     // thisInt, thisFloat, thisChar are the arguments
    myFunction(3, 4.5, 'q'); // can pass in constants as arguments too
}

1.3 Syntax


// for parameters in a function definition [type-modifier] type-descriptor [&] parameter-name // parameters in a function prototype can drop the parameter-name // no special syntax for argument- either use the variable name or a constant

1.4 Tips

2 Parameter Passing

There are two different modes for passing parameters to functions, call by value and call by referene.

2.1 Call By Value

2.1.1 Description

2.1.2 Example

This code uses call by value:

void myFunction(int someInt)
{
 someInt++; // change value of someInt
}
int main( )
{
  int myInt = 3;
   myFunction(myInt);
   cout << "After function call: " << myInt << endl;
}

Question: What will this function print?

Answer

2.1.3 Tips

2.2 Call By Reference

2.2.1 Description

2.2.2 Tips

2.2.3 Example

void myFunction(int& someInt)
{
 someInt++; // change value of someInt
}
 
int main( )
{
   int myInt = 3;
    myFunction(myInt);
    cout << "After function call: " << myInt << endl;
    return 0;
}

Question What will this program print?

Answer
void myFunction(const int& someInt)
{
 someInt++; // change value of someInt
}

int main( )
{
   int myInt = 3;
    myFunction(myInt);
    cout << "After function call: " << myInt << endl;
    return 0;
}

Question What will this program print?

Answer

2.2.4 Tips

3 Experiment

//*************************
// EXPERIMENT: what is the difference between
// call by value and call by reference?
// 
// Programmer: Chris Wild
// Date: 6 Sep 1999
//*************************

#include <iostream.h>

//** swap two integers
void swapByValue(int first, int second)
{
    int temp = first; // need a temporary place to hold swap

    first = second;
    second = temp;
}

void swapByRef(int&, int& ); 
// this is a function prototype - how do you know - because of the ';'
// it means that this function is defined elsewhere (see below)
// this is called a forward reference
// demonstrates two ways of defining low-level functions
// Also notice that we do not have to--mechanically--name the parameter in
// a function prototype


int main( )
{
   int myFirst = 3;
   int mySecond = 7;
   
   swapByValue(myFirst, mySecond);
   cout << myFirst << "#" << mySecond << endl;
   
   swapByRef(myFirst, mySecond);
   cout << myFirst << "#" << mySecond << endl;
   
}

void swapByRef(int& first, int& second)
{
    int temp = first; // need a temporary place to hold swap

    first = second;
    second = temp;
}

Hypothesis:

Observation

Try entering that code into “experiment.cpp”, compile it, and run it.