Professional Documents
Culture Documents
1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann
Thisiscalled________________________________. ModularProgramming
When creating a function, you must create its __________ . It consists of the following parts:
definition: Allfunctiondefinitions havethefollowing parts: Name Parameterlist Everyfunctionmusthaveaname Listofvariablesthatholdthevalues beingpassedtothefunction. Bodyofthefunction(setofstatements thatcarryoutthetaskofthefunction isperforming).Enclosedinbraces.
Body
ENGR 1200U Winter 2013 - UOIT
return type
name
parameter list
double cube_volume(double side_length) { double volume=side_length *side_length *side_length; return volume; } body
ENGR 1200U Winter 2013 - UOIT
Afunctionprototypeeliminatestheneedtoplacea functiondefinitionbeforeallcallstothefunction.
When a function is called, the program may send values into the function. Values that are sent in a function call are called _________.
Example
Yes. Sometimes there is a need for a function that tests an argument and returns a true or false value indicating whether or not a condition is satisfied.
bool isValid(int number) { bool status; if (number>=1&&number<=100) status=true; else status=false; return status; }
This code snippet shows an if/else statement that makes a call to the function:
Calling the isValid function:
int value=20; if (isValid(value)) cout<<"Thevalueiswithinrange.\n"; else cout<<"Thevalueisoutofrange.\n";
A local variable exists only while the function it is defined in is executing This is known as lifetime of a local variable
When the function begins, its parameter variables and any local variables it defines are created in memory When function ends, these variables are destroyed This means that any values stored in a functions parameters or local variables are lost between calls to the function.
Global constants are typically used to represent unchanging values that are needed throughout a program Example: Suppose a banking program uses a named constant to represent an interest rate.
If interest rate is used in several functions, it is easier to create a global constant, rather than a local named constant in each function
You cannot have two local variables with the same name in the same function
This applies to parameter variables as well A parameter variable, in essence, a local variable. Hence, you cannot give a parameter variable and a local variable in the same function the same name!
However, you can have a parameter or local variable with the same name as a global variable or constant
10
If a function is called more than once in a program, the values stored in the functions local variables do not persist between function calls
This is because local variables are destroyed when a function terminates Local variables are also recreated when the function starts again
Sometimes it is desirable for a program to remember what value is stored in a local variable between function calls.
This is accomplished by making the variable static Static variables are not destroyed when a function returns
They exist for the entire lifetime of the program, even though their scope is only the function in which they are defined
11
12
New programmers often have difficulty determining when an argument should be passed to a function by reference and when it should be passed by value
When an argument is a constant, it must be passed by value When a variable is passed as an argument should not have its value changed, it should be passed by value
This protects it from being altered
13
Here are three common instances when reference parameters are used:
When data values being input in a function need to be known by the calling function When a function must change existing values in the calling function When a file stream object is passed to a function
A Two or more functions may have the same name, as long as their parameter lists are different
Sometimes you will create two or more functions that perform the same operation, but use a different set of parameters, or parameters of different data types
Example:
Suppose there is a square function that uses a double parameter Also, suppose you also wanted a square function that works exclusively with integers and accepts an int as its argument
Both functions would do the same thing, return the square of their argument The only difference is the data type involved in the operation
14
If you were to use both these functions in the same program, you could assign a unique name to each function
Example: squareInt, and squareDouble C++, however, allows you to overload function names This means you may assign the same name to multiple functions, as long as their parameters lists are different
#include <iostream> #include <iomanip> using namespace std; int square(int); double square(double);
int square(int number) { return number*number; } double square(double number) { return number*number; }
int main() { int userInt; double userReal; cout <<"Enteranintegerandafloatingpointvalue:"; cin >>userInt >>userReal; cout <<"Herearetheirsquares:"; cout <<fixed<<showpoint <<setprecision(2); cout <<square(userInt)<<"and" <<square(userReal)<<endl; return 0; }
ENGR 1200U Winter 2013 - UOIT
15
The exit() function causes a program to terminate, regardless of which function or control mechanism is executing
#include <iostream> #include <cstdlib> using namespace std; void someFunction(); int main() { someFunction(); return 0; } //Neededtousetheexitfunctioninsomecompilers
void someFunction() { cout <<"Thisprogramterminateswiththeexitfunction.\n"; cout <<"Bye!\n"; exit(0); cout <<"Thismessagewillneverbedisplayed\n"; cout <<"becausetheprogramhasalreadyterminated.\n"; }
Program Output This program terminates with the exit function. Bye!
It is common to invoke a function repeatedly with the same argument value for a particular parameter. In such cases, you can specify parameters with a default argument
#include <iostream> VoidboxVolume(int length=1,int width=1,int height=1); using namespace std; void someFunction(); int boxVolume(int length,int width,int height) { returnlength*width*height; }
Program Output This program terminates with the exit function. Bye!
16