0% found this document useful (0 votes)
9 views19 pages

Designing Programs With Functions

The document outlines the design of programs using functions, emphasizing the creation of algorithms and pseudocode, as well as translating these into Python code. It includes practical tasks for developing programs that guide users through specific processes, such as disassembling an appliance or preparing coffee orders, along with exercises on defining functions and understanding local and global variables. Additionally, it highlights the importance of hierarchy charts and the distinction between local and global variables in programming.

Uploaded by

Yesha Kakkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

Designing Programs With Functions

The document outlines the design of programs using functions, emphasizing the creation of algorithms and pseudocode, as well as translating these into Python code. It includes practical tasks for developing programs that guide users through specific processes, such as disassembling an appliance or preparing coffee orders, along with exercises on defining functions and understanding local and global variables. Additionally, it highlights the importance of hierarchy charts and the distinction between local and global variables in programming.

Uploaded by

Yesha Kakkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Designing Programs

with Functions
Minds On
● Define a function that accepts 2 values, and prints its
quotient, product, and power
Minds On - Solution
● Define a function that accepts 2 values, and prints its
quotient, product, and power
Learning Goals and Success Criteria
❏ Devise a plan for a program by creating an algorithm and pseudocode
❏ Translate information from your algorithm and pseudocode into Python
language
❏ Define a function and develop a program containing multiple functions
❏ Identify and describe the difference between a local and global variable
Flowcharts
Top-Down Design

- Overall task is broken down into a series of subtasks


- Each subtask is examined to see if it can be broken down into more subtasks and this is repeated
until there are no more subtasks identified
- Once all the subtasks have been identified they are written in code

Programmer begins by looking at the topmost level of tasks and breaks it down into lower level tasks.

Can use hierarchy charts to depict the flow of these overall tasks and subtasks.
Hierarchy chart example
Practise Task 1

An appliance company wants their technicians to remember their instructions. The owner has asked you to develop
a program that displays the following instructions for disassembling the dryer appliance:

Step 1: Unplug the dryer and move it away from the wall

Step 2: Remove the six screws from the back of the dryer

Step 3: Remove the dryers back panel

Step 4: Pull the top of the dryer straight up

After each step, the user will be asked to press Enter to see the next step. The program should also contain a
greeting/opening message as well as a closing message.
Task

1. Design the algorithm in pseudocode (5 mins)


2. Create a hierarchy chart (5 mins)
3. Write the code (5 mins)
4. Check with a partner (5 mins)

Run in Python together


Practise Task 1 Sample Answer
Practise Task 2

A coffee shop wants a program to guide baristas in preparing customized coffee orders. The owner has
asked for the program to include the following instructions:
1. Choose Coffee Type: The barista must select the type of coffee (Espresso, Latte, Cappuccino).
2. Choose Add-ons: Ask the customer if they want any add-ons: (Milk, Sugar, Whipped Cream).
3. Cup Size Selection: The customer selects a cup size (Small, Medium, Large).
4. Display Summary: Show a summary of the customer’s choices.
5. Confirmation: Ask the barista to confirm before completing the order. If the order is incorrect, they
can go back and edit the choices.

For this example, we will not write the code, yet (since we haven’t discussed decision-making yet).

Instead, write 2-3 different hierarchy charts that seem reasonable and check with a classmate.
Practise Task 2 Sample Answers
Practise Questions

1. a) Define a function named f_1 which will print "Hello World!"

b) Add onto the function code by asking the user to input their names.

c) Add onto the function code by printing the name as part of the code

d) Call the function

2. a) Define a function named f_2 which will ask users to input the date

b) Within the function, call another function named f_3 that will output a story (3 lines of code to be
printed that you can choose that includes the date that was input by the user)

c) Define f_3

d) Call f_2
Local Variables

def main():

get_name()

print(‘Hello’, name)

def get_name():

name = input(‘Enter your name: ‘)

main()

#What happens here?


Local Variables

- Created inside a function and CANNOT be accessed by statements that are outside the function.
- Different functions can have local variables with the same names because the functions cannot see each other’s
local variables
- An error will occur if a statement in one function tries to access a local variable that belongs to another function
- Also cannot be accessed if it appears inside the function at a point before the variable has been created.
Global Variables

When a variable is created by an assignment statement that is written outside all the functions in a
program file, the variable is global.

Ex.

value = 200

def main():

print(value)

main()
Global Variables

Most programmers restrict the use of global variables or don’t use them at all because:

- Makes debugging difficult with longer codes


- Relies on the global variable and will have to redesign code to use the function in a different program
- Makes a program hard to understand
Global Constant

Define: A global name that references a value that cannot be changed. -> Don’t have to worry about hazards with
global variables

Python doesn’t allow you to create true global constants.

When you create a variable outside of a function without the keyword global, it cannot be changed inside the function.
-> Similar to what we’ve been doing.
Homework

CS Circles practice

CMU CS practice

You might also like