CIS22A - Notes for Week 8, 11/11-11/14

Announcements and Reminders


Topics

Terminology

Default Arguments

A default argument is a value that is automatically passed as a function argument when the argument is not provided by the function call.

Example

#include <iostream>
using namespace std;

int power(int,int = 2); // prototype with default argument

int main(void)
{
   cout << power(5) << endl;
   cout << power(2,10) << endl;
   cout << power(12345) << endl;
}


int power(int x, int y)
{
   int num = 1;
   for (int i = 1; i <= y; i++) num *= x;
   return num;
}

Notes

  • Default arguments should be placed in the function prototype.  If a prototype is not provided, then the default arguments must be placed in the function heading.
  • In the function argument list, mandatory arguments must preceed default arguments.
  • Default arguments may not be specified in both the function prototype and the heading of the function definition.
  • All of a function's arguments may have default values.
This is an error

void funk(int arg1 = 7, int arg2);

A function that is prototyped like this,

void funk(int x = 2, int y = 4, int z = 6);

may be called in 4 different ways,

like this,

funk(1,2,3);     // first argument = 1, second argument = 2, third argument = 3

or this,

funk(1,2);     // first argument = 1, second argument = 2, third argument = 6

or this,

funk(1);     // first argument = 1, second argument = 4, third argument = 6

or this,

funk();     // first argument = 2, second argument = 4, third argument = 6

Example

Pass By Reference

A reference variable is an alias for another variable, defined elsewhere in the program.  Reference variables are commonly used for function arguments or return values.  The use of reference variables as function argument avoids the requirement to pass a variable's address to a function and avoid the necessity to dereference variable addresses.

Reference to const

A reference to a const is used as a function argument to disallow the function from changing the argument value.

Example

#include <iostream>
using namespace std;

// function prototypes
void update_salary(double& sal)
;                          // reference argument
void display_salary(const double &sal);         // reference to const argument

int main()
{
   double salary = 50000.;
   display_salary(salary);
   update_salary(salary);
   display_salary(salary);
}

void update_salary(double& sal)
{
   sal *= 1.1;
}

void display_salary(const double &sal)
{
   cout << sal << endl;
}


****** OUTPUT ******

50000
55000

The swap function

int main()
{
   int x = 2, y = 3;
   swap(x,y);
   cout << x << ' ' << z << end;   // prints 3 2
   ...
}

Function Overloading

Function overloading is the use of multiple functions having the same name with different arguments.

Example

#include  <iostream>
#include  <cmath>
using namespace std;

const double PI = 3.141592654;                     // named constant

// function prototypes
double area(double radius);                        // area of a circle
double area(double length, double width);          // area of a rectangle  
double area(double s1, double s2, double s3);      // area of a triangle   

int main()
{
   // circle
   double radius = 5.0;
   cout << area(radius) << endl;

   // rectangle
   double length = 6.0, width = 4.0;
   cout << area(length,width) << endl;

   // triangle
   double side1 = 3, side2 = 4, side3 = 5;
   cout << area(side1,side2,side3) << endl;
}

// area of a circle
double area(double radius)
{
   return PI * radius * radius;
}

// area of a rectangle    
double area(double length, double width)
{
   return length * width; 
}

// area of a triangle  
double area(double side1, double side2, double side3)
{
   double s = .5 * (side1 + side2 _ side3);
   return sqrt(s * (s-side1) * (s-side2) * (s-side3));
}


*****   OUTPUT  *****

78.5398
24
6

Notes

  • Function overloading does not involve the function return type.
  • The following function overloading is not allowed:
void funk(int, int = 0);
void funk(int);

Example

More Function Examples

Deal 5 unique cards

Clock Using Reference Variables

Date Application Using Reference Variables


Videos

Functions

Creating Functions That Use Parameters

Functions That Use Multiple Parameters
default argument

pass by reference
reference to const

function overloading
overloaded function