University of WashingtonCM 598lab_7 Starting Out with Programming Logic and Design 1Lab 7: FunctionsThis lab accompanies Chapter 6 of Starting Out with Programming Logic & Design. Lab 7.1 – Functions and Pseudocode Lab 7.2 – Functions and Flowchart Lab 7.3 – Python Code and Random Lab 7.4 – Programming Challenge 1 – Math Test Lab 7.1 – Functions and PseudocodeCritical ReviewYou ha
...[Show More]
lab_7 Starting Out with Programming Logic and Design 1
Lab 7: Functions
This lab accompanies Chapter 6 of Starting Out with Programming Logic & Design.
Lab 7.1 – Functions and Pseudocode
Lab 7.2 – Functions and Flowchart
Lab 7.3 – Python Code and Random
Lab 7.4 – Programming Challenge 1 – Math Test
Lab 7.1 – Functions and Pseudocode
Critical Review
You have been coding with modules in pseudocode and functions when using Python.
You modules in pseudocode can be made into functions by returning a value.
A function is a special type of module that returns a value back to the part of the program
that called it.
Most programming languages provide a library of prewritten functions that perform
commonly needed tasks.
Library functions are built into the programming language and you can call them as
needed. They are commonly performed tasks.
Help Video: Double click the file to view video
Writing Your Own Function that Returns an Integer
Step 1: A function contains three parts: a header, a body, and a return statement. The
first is a function header which specifies the data type of the value that is to be returned,
the name of the function, and any parameter variables used by the function to accept
arguments. The body is comprised of one or more statements that are executed when the
function is called. In the following space, complete the following: (Reference: Writing
Your Own Functions, page 225).
a. Write a function with the header named addTen.
b. The function will accept an Integer variable named number.
c. The function body will ask the user to enter a number and the add 10 to the
number. The answer will be stored in the variable number.
d. The return statement will return the value of number.
Function Integer addTen (Integer Number)
Display “Enter a number:”
Input number
Set number = number + 10
Return number
Step 2: In the following space, write a function call to your function from Step 1.
Starting Out with Programming Logic and Design 2
Set number = addTen (number)
Writing Your Own Function that Returns a Boolean Value
Step 1: A Boolean function will either return a true or a false value. You can use these
functions to test a condition. They are useful for simplifying complex conditions that are
tested in decision and repetition structures. In the following space, complete the
following: (Reference: Returning Boolean Values, page 238).
a. Write a function with the header named gender.
b. The function will accept a Boolean variable named answer.
c. The function body will ask the user to enter their gender into the variable type and
then determine if they are male or female with an if statement.
d. The return statement will return the value of answer.
Function a. Boolean a. gender (b. Boolean Answer)
Declare String type
Display “Enter your gender (male or female):”
Input c.type
If (c.type == “male”) then
answer = False
Else
answer = True
End If
Return d.answer
Step 2: In the following space, write a function call to your function from Step 1.
Set answer = gender (answer)
Using Mathematical Library Function: sqrt
Step 1: The sqrt function accepts an argument and returns the square root of the
argument. In the following space, complete the following: (Reference: The sqrt
Function, page 240).
a. Declare a variable named myNumber and a variable named squareRoot of the
data type Real.
b. Ask the user to enter a number of which they want to find the square root. Store
the input in myNumber.
c. Call the sqrt function to determine the square root of myNumber.
d. Display the square root to the screen.
Declare Integer a.myNumber
Declare Real a.sqaureRoot
Display “Enter a number:”
Input b. myNumber
Set c.squareRoot = sqrt (myNumber)
Starting Out with Programming Logic and Design 3
Display “The square root is”, d.squareRoot
Using Formatting Functions
Step 1: Most languages provide one or more functions that format numbers in some way.
A common use of formatting functions is to format numbers as currency amounts. While
a specific programming language will have its own name for formatting currency, use the
function currencyFormat for pseudocode. In the following space, complete the
following: (Reference: Formatting Functions, page 246).
a. Declare a variable named subtotal, a constant variable named tax set to the rate
of .06, and a variable named total.
b. Ask the user to enter the subtotal. Store the input in subtotal.
c. Calculate the total as subtotal + subtotal * tax.
d. Make a call to the currencyFormat function and pass it total. Since you are not
displaying it on this line, simply set the return value to total.
e. Display the total to the screen.
Declare Real a. subtotal
Declare Constant Real a. tax = .06
Declare Real a. total
Display “Enter the subtotal:”
Input b. subtotal
Set c. total = subtotal + subtotal * tax
total = d. currencyFormat (total)
Display “The total is $”, e. total
Starting Out with Programming Logic and Design 4
Lab 7.2 – Functions and Flowcharts
[Show Less]