LECTURE 10 Functions Part 4

advertisement
LECTURE 10
Functions Part 4
REVIEW
Pass By Value
 The local parameters are copies of the original arguments passed in
 Changes made in the function to these variables do not affect originals
Pass By Reference
 The local parameters are references to the storage locations of the original arguments passed in.
 Changes to these variables in the function will affect the originals
 No copy is made, so overhead of copying (time, storage) is saved
CONST REFERENCE PARAMETERS
The keyword const can be used on reference parameters.
void Func3(const int& x);
// pass by const reference
This will prevent x from being changed in the function body.
General format:
const typeName & variableName
This establishes variableName as a reference to a location that cannot be changed
through the use of variableName.
CONST REFERENCE PARAMETERS
This would be used to avoid the overhead of making a copy (especially of a large
item), but still prevent the data from being changed.
Since the compiler will guarantee that the parameter value cannot change, it IS legal
to pass in any R-value in this case:
int num = 5;
Func3(num);
// legal
Func3(10);
// legal
Func3(num + 50);
// legal
Code example illustrating a function with a const reference parameter.
RECURSION
In a programming context, recursion refers to when a function makes a call to itself.
This is a perfectly valid, and in some cases, useful way to structure a program.
Look at this loop:
for(int i=0;
i<10; i++) {
cout << "The number is: " << i << endl;
}
RECURSION
The output of the for loop is as follows:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
RECURSION
#include <iostream>
using namespace std;
What if we moved the cout
statement to a function by itself?
void numberFunction(int);
int main() {
for(int i=0; i<10; i++) {
numberFunction(i);
}
return 0;
}
void numberFunction(int i) {
cout << "The number is: " << i << endl;
}
RECURSION
Now, instead of returning to the
main() function after every call
to numberFunction, we’ll replace
the looping functionality with
recursion.
#include <iostream>
using namespace std;
Void numberFunction(int);
int main() {
int i = 0;
numberFunction(i);
return 0;
}
void numberFunction(int i) {
cout << "The number is: " << i << endl;
i++;
if(i<10) {
numberFunction(i);
}
}
EXERCISE 1
Write a program with a function that takes two integer parameters, adds them
together, then returns the sum. The program should ask the user for two numbers, then
call the function with the numbers as arguments, and tell the user the sum.
EXERCISE 2
Overload the previous function to accept doubles.
EXERCISE 3
Basically the same as exercise 1, but this time, the function that adds the numbers
should be void, and takes a third, pass by reference parameter; then puts the sum in
that.
EXERCISE 4
Write a function that draws a rectangle on the screen. The user can specify the height
and width of the rectangle or just the width. If they only specify the width, a default
length of 5 will be used. They can also specify the character used to draw the
rectangle, but if not, the character ‘*’ will be used. Spaces should appear between
characters in the same row. For example, if the width is 3, length is 2, and character
is ‘*’, the program will print:
* * *
* * *
EXERCISE 5
Write a recursive function that finds the nth integer of the Fibonacci sequence. Then
build a minimal program to test it. The user should be able to call the function as
Fibonacci(n).
The Fibonacci sequence is given by 𝐹𝑛 = 𝐹𝑛−1 + 𝐹𝑛−2 .
The first 8 integers are 1, 1, 2, 3, 5, 8, 13, …
Download