At the end of this lesson, you will be able to:
understand and create your own function that has a return value
What is the difference between a parameter and an argument?
What does it mean to "pass by value"?
For the following function in Python:
a) What is the name of the function?
b) How many parameters does it take?
c) State each parameter type.
d) What does the function return?
e) State what will occur by the following:
print_name("Sophie", "A", "Student")
4. For the following function in C++:
a) What is the name of the function?
b) How many parameters does it take?
c) State each parameter type.
d) What does the function return?
e) State what will occur by the following:
PrintStudent("Sophie Student", 16)
5. Create a function in C++ called CalcPerimeter() that accepts the length and width as decimal parameters. It then calculates and displays the perimeter of a rectangle with the given length and width.
6. Write the code in C++ to call the above function by passing it userLength and userWidth.
functions with parameters
from the online book Computer Based Problem Solving by Patrick Coxall, read:
return values are the value that is passed back, or returned, from a function
in C++, if nothing is returned from a function, we write a return value of void
now that we know how to use a return statement, we will no longer print out results in a function (like we have in the last few units) we will always return the value
Create a program called "Grade Program" that has a function that takes a level and returns the middle percentage mark.
Name the function "calc_mark(level)" in Python and "calcMark(string level)" in C++
Use the switch statement you learned in Unit 3. (You will have do to a workaround in Python.)
It will accept the level as a string (see the Ontario rubric chart here)
It will return the middle percentage mark.
Remember:
if you have a flowchart that repeats a similar pattern
put the first couple statements
then use "..."
then put the last statement and any error case
only place one return statement at the very end of the function, not several inside an if statement
if "invalid" input is passed to the function (a string that is not a valid level), return a "-1"
in groups of 2, do the following on the board for today's daily assignment:
Top-Down Design
Flow Chart
Pseudocode
Test Cases
complete the Daily Assignment section in Hãpara Workspace for this day
if Hãpara is not working, make a copy of this document
move it to your IMH-ICS folder for this course
recreate the same program in C++
use the switch statement you learned in Unit 3