Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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.

2 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.

3 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).

4 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.

5 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.

6 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.

7 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;

8 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.

9 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.

10 0123 4567 a

11 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].

12 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;

13 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];

14 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.

15 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);

16 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.


Download ppt "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."

Similar presentations


Ads by Google