16
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and int max(int x, int y, int z) are different functions, as are int hash(int x) and int hash(char c). By convention functions with the same name do similar work.

Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Embed Size (px)

Citation preview

Page 1: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Function Overloading

Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and int max(int x, int y, int z) are different functions, as are int hash(int x) and int hash(char c).

By convention functions with the same name do similar work.

Page 2: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Pitfalls

Having two functions with the same name and number and types of arguments but different return types is an error.

Since argument types may be converted to compatible types when a function is called, you must be careful when two functions have compatible types.

Page 3: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Example

Consider the two functions:

int foo(int x, int y) { return x < y ? x : y;}double foo(double x, double y) { return x – y;}

foo(2, 3) calls the first function, foo(2.0, 3.0) calls the second function, as does foo(2, 3.0).

Page 4: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Default Values

The last parameters in a function may be given default values if they are call-by-value parameters. The syntax is:int foo(int a, int b = 10) {}

If foo is called with two arguments, then a and b are both set to the corresponding values. If called

with one value, then a is set, and b is set to 10.

Page 5: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Review of Functions

Functions may have a return type and can be used in expressions, or have return type void and are called as statements.

Functions may have call-by-value and call-by-reference parameters. The former are used for input parameters only.

A function with a return type must have a return statement to pass back a value.

Page 6: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Review of Functions (cont'd)

Functions are used in program design to implement steps as the programmer breaks the problem down into pieces.

Functions may be overloaded, i.e., two functions may have the same name.

Functions may have default values.

Page 7: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Problem

Suppose we want to read in five ints and then print them back out, only in the reverse order in which they are read. No problem:

int a1, a2, a3, a4, a5;cin >> a1 >> a2 >> a3 >> a4 >> a5;cout << a5 << “ “ << a4 << “ “ << a3 << “ “ << a2 << “ “ << a1 << endl;

Page 8: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Another Problem

Now suppose we want to read in 100 ints (or 1000 ints) and print them back out in the reverse order read in. Problem, or at least, boring.

Page 9: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

A Solution

Instead of using five, or 100, or 1,000 variables to store the input, it would be better to use just one variable that has five, or 100, or 1,000 parts. We can do this in C++ by using an array. An array variable is a single variable, but can hold multiple items (all of which may be the same type). The different items are referred to by number. Picture a box with numbered compartments.

Page 10: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

0 1 2 3

4 5 6 7

a

Page 11: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Array Syntax

Instead of writing a1, a2, a3, ... , we write a[1], a[2], a[3], .... (read a-sub-1, a-sub-2, a-sub3).

The “a” refers to the entire box, and the subscript refers to the particular item in the box.

The advantage of this notation is that the subscript can be a variable, or even an expression, e.g., a[i].

Page 12: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Solution Revisited

So, if we want to read in 100 numbers and print them out backwards, we can use an array to store the values, and two loops – one to read in the numbers and one to print them out. Problem solved

for(int i = 0; i < 100; i++) cin >> a[i];for(int i=99; i >= 0; i—) cout << a[i] << “ “;cout << endl;

Page 13: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Arrays

An array is a data object that can hold a number of homogeneous values (all of the same type)

The elements of a particular array are numbered, starting at 0.

An array is a single variable, that is, the identified is associated with the big box.

Arrays must be declared before they are used, and the declaration includes the size: int a[100];

Page 14: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Example

Problem: Read in 20 grades (from 0 to 100). Calculate and print out the average grade and the standard deviation. The formula for the standard deviation is sqrt((∑i=1,n (grade

i – average)2) /20).

We will need two loops: one to read in the grades

and compute the average, and one to compute the

sum necessary for the standard deviation. Since we

can only read in the grades once, we must save their

values in an array.

Page 15: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

A Solution

int grades[20], sum = 0; for(int i=0; i<20; i++) { cin >> grades[i]; sum += grades[i]; } double ave = ((double)sum)/20; double sum2 = 0.0; for(int i=0; i<20; i++) sum2 += pow(grades[i] - ave, 2); double sd = sqrt(sum2/20);

Page 16: Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and

Exercise

Read in a list of positive ints (with the end indicated by a non-positive int) and then say whether or not the list is the same backward as forward (a palindromic list). For example: 1 2 3 4 3 2 1 is, but 1 2 3 4 4 1 3 isn't.