Parameters and Arguments

Chris Wild & Steven Zeil

Last modified: May 28, 2014

Contents:
1. Description
2. Example
3. Syntax
4. Tips
5. Call By Value
5.1 Description
5.2 Example
5.3 Tips
6. Call By Reference
6.1 Description
6.2 Example
6.3 Tips
7. Experiment

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.

2. Example

void myFunction(int anInteger, float aFloat, char aChar)
// anInteger, aFLoat, aChar are the aliases (parameters) for the eventually 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
}

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
 

4. Tips

5. Call By Value

5.1 Description

5.2 Example

void myFunction(int someInt)
{
 someInt++; // change value of someInt
}
int main( )
{
  int myInt = 3;
   myFunction(myInt);
   cout << "After function call: " << myInt << endl;
}
// what will this program print?
// if you didn't answer "after function call: 3", 
// then you don't understand call by value

5.3 Tips

6. Call By Reference

6.1 Description

6.2 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;
}
// what will this program print?
// if you didn't answer "After function call: 4",
 // then you don't understand call by reference
 // see also the on parameter passing

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;
}
// This program should not compile because you lied to the compiler
 // and went and modified a "const" object.

6.3 Tips

7. 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 - becuase 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 funcitons|
// Also notice that we do not have to 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: