Download presentation
Presentation is loading. Please wait.
1
Value returning Functions
Lesson xx In this module, we’ll talk about functions that return a value.
2
Objectives Review void returning functions
Write a program that returns a value. We plan to do the following this module: 1) Review void returning functions 2) Write a program that demonstrates value returning functions.
3
void Returning Function
void prtStar ( int num) { int i; cout << endl; for (i = 0; i < num; i++) cout << "*"; } This is the prtStar() function from the previous module which prints num # of*s. In other words, if num is 11 this code prints 11 *s. If num is 66, 66 *s will be printed. Let’s examine the header, we have void prtStar(int num). The keyword void means that the function returns no value. There are times when you want the function to compute a value and send it back to the calling module. This is called a value returning function.
4
Syntax of a Value Returning Function
return type function name ( arguments) { //body } This is the syntax of a value returning function. You need a header and the body of the code goes inside a set of {}s. The header has 3 parts: the return type, the function name and the arguments inside a set of ()s.
5
Example of a Value Returning Function
Return type Function name Arguments int computeXvalue (float a, char c) { int x; //body return x; } Here is an example of how you write a value returning function. Our function name is computeXvalue. The function takes 2 arguments, the 1st is a float and the 2nd is a char. When the function is done, it returns an int back to the calling module. Just a note here, the last statement of the function is return x; the data type of x is an int and that is what you use for the return type. If the data type of x is a long, you put long for the return type of the function. Enough talking, let’s look at an example in real life.
6
Program Definition Write a program that:
Reads in a minimum and maximum value in main( ). Send minimum and maximum into a function as arguments. The function should generate a random number between minimum and maximum. Return the random number back to main() where it is printed Let’s write the following program to demonstrate value returning functions: 1) Read in a minimum and maximum value in main( ). 2) Send minimum and maximum into a function as arguments. 3) The function should generate a random number between minimum and maximum. 4) Return the random number back to main() where it is printed
7
Program Output Here is the output to this program that uses a value returning function.
8
Code int randomnum ( int min, int max ) { int numbtween;
#include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } int randomnum ( int min, int max ) { int numbtween; int diff = max - min; numbtween = rand () % diff + min; return numbtween; } Here is the entire program using a value returning function. Let’s go through the code now.
9
Function Prototype #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } Let’s start from the beginning and look at the code in main( ). If we are going to call a function, we need a function declaration which is highlighted in blue. Notice that in this example, the prototype is inside of main( ) whereas in the previous modules, it was outside of main( ). You can write the prototype in either location. If you write it outside of main( ), it is a global declaration. Inside of main ( ) it is a local declaration
10
Declaration #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } The code in red is the declaration of our variables. small is used to store the smaller #, and big is used to store the larger #. rn is where we store the random # generated by the function.
11
Input #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } This code in red prompts the user for 2 #s and reads them into the variable small and big.
12
Function Call #include<iostream> using std::endl; using std::cin; using std::cout; int main() { int randomnum ( int min, int max ); int small, big, rn; cout << "Enter 2 #s & I will generate a “; cout <<“random # from min to max "; cin >> small >> big; rn = randomnum ( small, big); cout << "The number in between is " << rn << endl; return 0; } rn = randomnum (small, big ); is how you call the function. This is how you interpret the statement: call the randomnum function and send in small and big as arguments. When the function returns the random number will be stored in rn. The cout statement prints out the random number.
13
Value Returning Function
rn = randomnum ( small, big); int randomnum ( int min, int max ) { int numbtween; int diff = max - min; numbtween = rand () % diff + min; return numbtween; } Here is the code for the randomnum( ) function. The code in red is the call to the function from main( ). The blue arrows show you what happens when the function is called. The contents of small is stored in the local variable min and the # in big is stored in max. The statements in the function are executed. When the computer encounters the statement: return numbtween; the value in the variable numbtween is transferred back to main () into the variable rn.
14
randomnum( ) Function int randomnum ( int min, int max ) {
int numbtween; int diff = max - min; numbtween = rand () % diff + min; return numbtween; } Now that you know the mechanics of how a function works, let’s look at the code in the randomnum( ) function. The 1st line of the function declares a local variable called numbtween. We use this variable to store the random number between min and max. The 2nd line of the function subtracts min from max and stores the result in diff. diff represents the number of different random numbers from min to max. Here is an example, suppose min = 3 and max = 7 , the random numbers that are possible between 3 and 7 are 3,4,5,6 and there are 4 of them.
15
Built-in rand ( ) Function
int randomnum ( int min, int max ) { int numbtween; int diff = max - min; numbtween = rand () % diff + min; return numbtween; } The built-in rand() function generates a random number from 0-32,767. In our program, we want a random # from min to max. By doing the calculations rand () % diff + min, we can get a random # in the correct range. We store this number in numbtween and in the last line of the function we return this value back to main. There we have it, a function that returns a value.
16
Summary Review void returning functions
Write a program that returns a value In this module, we reviewed void retuning function and then went on to show you value returning functions. In the next module, we’ll do some more work with functions. We’ll talk about pass by value and pass by reference.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.