CIS 150 Functions II

Objectives

  • Use function overloading
  • Discuss variable scope and lifetime
  • Discuss differences between passing by-value vs. by-reference
  • Use reference variables
  • Use default values in a function call

Function overloading

Function overloading is when two functions have the same name, but a different parameter list (based on the data types, number, and ordering of the parameters).

The following is an example where two functions with the same name can perform very different calculations based on having different parameter lists.

int funcx(int n1, int n2) { return n1 + n2; } double funcx(double n1) { return sqrt(n1); }

Variable scope and lifetime

Function parameters and local variables defined within a function are only seen within that function. That's called the variable's scope.

Function parameters and local variables come into existence when a function is called and go away when a function returns. That's called the variable's lifetime.

This means that a variable or parameter defined within a function can not be referenced outside of that function. A function must rely on ite parameters to get information in to the function, and can return only a single element when it returns.

Passing by value

Normally, a copy of the argument values is sent to a function when it is called. This is called pass by value. It means that the function can do anything it wants with the arguments and not have any effect on the calling program. Example:

void func1(int x) { // Note that this x and the variable x in main() are two different variables. // They can have the same name or different names. It doesn't matter. x = 12; // The local x was changed - but not the one in main() } int main() { int x = 7; func1(x); cout << x << endl; // will display 7 return 0; }

Passing by reference

Sometimes you want to let a function change the value of the arguments you send it. You can pass by reference to do that. Example:

void func1(int& y) { // The & makes the parameter a reference variable // Note that this y and the variable x in main() are basically the same variable. // They can have the same name or different names. It doesn't matter. y = 12; // The local y was changed - which is also the x in main() } int main() { int x = 7; func1(x); cout << x << endl; // will display 12 return 0; }

Passing by reference (the old way)

You can also pass by reference using pointers into memory. Example:

void func1(int* p) { // Now p is a pointer to an int in memory *p = 12; // The location in memory referred to by p was set to 12 } int main() { int x = 7; func1(&x); // This sends the address in memory of x to the function cout << x << endl; // will display 12 return 0; }

Default values

You can specify default values for parameters. If a value isn't supplied, then the default value will be used. One important rule is that the missing values must fill in from the rightmost position. You can not have a supplied calue coming after a missing value in a function call. Example:

int mult(int n1, int n2 = 10) { // n2 will be 10 if no value is supplied return n1 * n2; } int main() { cout << mult(3, 5) << endl; // will display 15 cout << mult(3) << endl; // will display 30 (using the default value) return 0; }