Title: Value Returning Functions
1Value Returning Functions
- // Function prototype
- int Largest(int num1, int num2, int num3)
- Function Name
- Type
- Parameters
- Type of parameters
- Formal parameters
- Actual parameters
- float sqrt(float x)
- char myFunction(float param1, int param2,
- char param3)
- -----
2Function Calls
- // Function prototype
- int Largest(int num1, int num2, int num3)
- float sqrt(float x)
- char myFunction(float param1, int param2,
- char param3)
- // Function calls
- max Largest(score1, score2, score3)
- // Must follow the prototype!
- // Do not include types!
- cout
- sqrt(delta)
- // Error
- // Must use the return value
- chaVal myFunction(50.5, 143, M)
3VOID Functions
- void DisplayResult(float avg, float max, float
min) - // Function type void (does not return any
value) - // Decompose a program into smaller functions
- // Programming Rule each function can have at
most 30 lines! - int main()
-
- float score, avg, highest, lowest
- int numScores, loopCount 0
- // input scores
- // Compute avg, highest and lowest
- // output results
- DisplayResult(avg, highest, lowest)
- return 0
-
4VOID Functions
- void DisplayResult(float avg, float max, float
min) - // Programming Rules Each function must have a
description! - // The function displays avg, max and min in
required format. - void DisplayResult(float avg, float max, float
min) -
- cout
- cout
- avg
- max
- min
- // return control, no value
- return
-
- -----
5- Functions without Parameters
- void UserInstruction()
- int main()
-
- float score, avg, highest, lowest
- int numScores, loopCount 0
- UserInstruction()
- // input
- // process
- DisplayResult(avg, highest, lowest)
- return 0
-
6- // Programming Rules Each function must have a
description! - // The function displays user instructions.
- void UserInstruction()
-
- cout
- section "
-
- number of "
-
- range of "
-
-
-
- return
-
7Example
- void DisplayMenu()
- int main()
-
-
- DisplayMenu()
-
-
- void DisplayMenu()
-
- cout
-
- return
8Scope of Variables
- The region of code where it is legal to
reference (use) an identifier. - Local Scope
- Global Scope
- -----
9Code Block
- Between a matching pair of braces
- The body of a function
- int main()
-
- int alpha 10
- // A block for if statement
- if (alpha 3)
-
- int n
- cin n
- alpha 3
-
- return 0
-
- -----
10Local Scope
int main() int alpha 10 // A code
block if (alpha 3) int n
cin n alpha n cout n
The scope of an identifier declared inside a
block extends from the point of declaration to
the end of that block. 11Global Scope
- The scope of an identifier declared outside all
functions (and classes) extends from the point of
declaration to the end of the entire source file.
- Programming Rules
- No global variables!
12Class Scope
13Scope of Function Parameters
- Formal parameters
- Local scope
- Same as local variable
- Cannot reference it outside the function
- Receive values on function call
- Actual parameters (no global variables)
- Local scope
- Cannot reference it inside the called
function
14Example
// Precondition op is C or S //
Postcondition the cube of // num is computed
when op is // C, and the square of num // is
computed when op is S. int DoIt(int num, char
op) if (op C) result pow(num,
3) else result pow(num, 2)
return result // Assume is
included // What is wrong?
- int DoIt(int num, char op)
- int main()
-
- int base, result
- char choice
- cout
- cin base
- cout
- cin choice
- while (choice ! C choice ! S)
-
- cout
- cin choice
-
-
- result DoIt(base, choice)
15Precondition and Postcondition
// Precondition op is C or S //
Postcondition the cube of // num is computed
when op is // C, and the square of num // is
computed when op is S. int DoIt(int num, char
op) int result if (op C)
result pow(num, 3) else result
pow(num, 2) return result
- int DoIt(int num, char op)
- int main()
-
- int base, result
- char choice
- cout
- cin base
- cout
- cin choice
- while (choice ! C choice ! S)
-
- cout
- cin choice
-
- result DoIt(base, choice)
16Parameter Names
- Meaningful names
- Formal and actual parameters can have the same
name - They are different variables in different scopes
- Normally they have different names
17Trace in HiC
- Largest.cpp
- Local Variables
- Parameters
- Same name
- -------
18Lifetime of a Variable
- Lifetime
- The period of time during program execution
when an identifier has memory allocated to it. - Automatic variables
- A variable for which memory is allocated and
deallocated when control enters and exits the
block it is declared. - Static variables
- A variable for which memory remains allocated
throughout the execution of the entire program.
19Program 2
- Due Date Monday, Feb 26
- Grace Date Thursday, Mar 1
- Individual Assignment
- OK to get LIMITED help from others
- DO NOT work as a group
- DO NOT copy from others
- DO NOT let others copy your code
- Program Plagiarism
-