Lecture Programming in C++ - Chapter 7: Functions. On completion of this chapter students will know how to: Define and call functions, determine the scope of a variable, pass values by reference, overload functions, create random numbers.
Trang 1Chapter 7 Functions
Trang 2Code group that performs single task
Specification refers to what goes into and out of function
Trang 4Need three things to use function
– Function declaration (prototype)
Indicates function exists and describes it Must be done before use of function
– Function call (invocation)
Causes control to transfer from function to function
– Function definition
Header line followed by function body enclosed in braces
Lesson 7.1
Trang 5Indicates function name, return type and types of values passed to function
Trang 6Transfers program control to functionGeneral form
Trang 7ftype fname (atype aname, atype aname) {
…
…
}
Trang 8mass should be declared
an integer and velocity
a double in program
Trang 9– if within, can only be called from function where declared
Function declaration MUST appear before call
Lesson 7.1
Trang 10– Can prevent lost and maintain
Called multiple times, allocated and freed repeatedly
Lesson 7.1
Trang 13else { return (b); }
Trang 14Returning Value From main
void type function do not return values
– control transfers back to calling function automatically
Trang 15Default
Function called
– Memory set aside and value copied into new memory location
– Calculations done inside function using value – When function finished executing, memory location destroyed
Lesson 7.2
Trang 16Use argument list to allow function to
directly use calling function's values
Reference symbol & required in function declaration and header
– Indicate arguments that will have values
modified
– Create aliases for original variable names
Lesson 7.3
Trang 17b c
d e
Trang 18Refers to region in which declaration is activeThree kinds of scope
Trang 19for (int n=0;n<8;n++) {
}
Function main ( ) int x;
for (int j=0;j<8;j++) {
Trang 20Allows manual modification to scope and storage rules
Lesson 7.5
Trang 21Variable maintains storage space and value after function finishes executing
Trang 22– With extern from any file
Lesson 7.5
Trang 23Multiple files help manage large programsEach compiled separately then linked to
create executable file
Share variable
– Variable declared as global without specifier in one file
– Other files extern type variable;
Lesson 7.5
Trang 25values: 15.0 for second argument and 8 for third
Cannot specify first and third and use default for second – must have all defaults last!
Lesson 7.6
Trang 26Defining two or more functions with same name
Each function takes different number of arguments
– C++ knows which function is being called by counting number of arguments used
– Should not use default arguments since
ambiguity could result
Lesson 7.7
Trang 28rand( ) and srand ( ) work together
– srand ( ) automatically "seeds" function rand ( ) – Functions linked through global variable
Trang 30Define and call functions
Determine the scope of a variablePass values by reference
Overload functions
Create random numbers
Chapter 7
Learned how to: