
Subroutines
Presentation
•
Computers
•
10th Grade
•
Medium
Roy Duguid
Used 20+ times
FREE Resource
14 Slides • 11 Questions
1
Do Now:
Teams > Coding > Subroutines > Do Now
2
# Length: Find number of characters in a string print(len("hello world"))
# Output: 11
# Case Conversion text = "hello world" print(text.upper()) # UPPERCASE -> HELLO WORLD print(text.title())
# Title Case -> Hello World
# Slicing word = "computer" print(word[0:3])
# Output: com
# Substring Search print("cat" in "concatenate")
# Output: True
# Find Position print("banana".find("a")) # Output: 1
3
Big Question
How do we define a subroutine?
How do we describe the purpose of parameters in subroutines?
How do we use procedures that accept arguments through parameters?
Key words: subroutine, function, procedure, parameter, argument
4
Sub programs are used to avoid unnecessary code duplication in
programs and to make programs easier to read.
Sub Programs
© ZigZag Education, 2020
Procedures
A sub program is a section of code that performs a specific task and
can be called when needed at any point in the program.
There are two different types of sub program:
Functions
5
A procedure is a sub program that doesn’t return a value back
to the main program.
Procedures
© ZigZag Education, 2020
def message():
print("Computer")
print("Science")
print("Rocks")
message()
This is an example procedure that will output the text "Computer",
"Science", "Rocks" whenever it is called.
A procedure or function won’t run unless it is called.
Output
Computer
Science
Rocks
Procedure call
6
Sometimes procedures and functions need data in order to carry out
their job. This is where parameters come in.
Parameters
© ZigZag Education, 2020
def areaCalc(width,height):
area = width * height
print(area)
areaCalc(10,5)
Parameters are like placeholders for values that a procedure or
function expects to be passed when it is called.
In this example the values 10 and 5 are passed into the Width and
Height parameters when the areaCalc procedure is called.
Output
50
Parameters
width
10
height
5
area
50
7
A function is a sub program that returns a value to the main program.
Functions
© ZigZag Education, 2020
def areaCalc(width,height):
area = width * height
return area
print(areaCalc(10,5))
The returned value takes the place of the original function call in the
main program.
This is the areaCalc sub program written as a function.
Output
50
width
10
height
5
area
50
8
Where a variable can be accessed within a program depends on the
variable scope. There are two types of variable:
VariableScope
© ZigZag Education, 2020
def speedCalc(distance, time)
speed = distance / time
return speed
answer = speedCalc(90, 2)
Local
Declared within a sub program and only accessible within
that sub program.
Global
Declared outside any sub programs and accessible
throughout the program, including inside sub programs.
Local Variable
Global Variable
This example
program features
a procedure to
calculate speed.
It uses both a
local and a global
variable.
Global
Local
9
10
11
12
13
Questions ahead....
14
Multiple Choice
Which of the following is not an example of a subroutine?
Procedure
Function
List
15
Multiple Select
What is the purpose of using subroutines in your programming? (select more than 1)
Avoiding repeated sections of code
Making your code shorter and easier to read
Enabling you to split your code into sections
Avoiding making errors in your code
16
Multiple Choice
What is the name of the subroutine?
happyBirthday
person
def
17
Multiple Choice
What is the name of the parameter?
happyBirthday
person
def
18
Multiple Choice
What is the name of the subroutine?
addNumbers
num1
num2
total
19
Multiple Choice
num1 and num2 are the parameters
True
False
20
Multiple Choice
Which subroutine will return a value back to the program?
Procedure
Function
21
Multiple Choice
Which subroutine will not return a value back to the program?
Procedure
Function
22
Multiple Choice
A procedure can return a value to be used later in the program
True
False
23
Multiple Choice
A function can return a value to be used later in the program
True
False
24
Multiple Choice
What is the name of the subroutine?
username
firstName
secondName
25
Activity 1
Do Now:
Teams > Coding > Subroutines > Do Now
Show answer
Auto Play
Slide 1 / 25
SLIDE
Similar Resources on Wayground
20 questions
Algoritme Dasar
Presentation
•
10th Grade
18 questions
SISTEM KOMPUTER
Presentation
•
11th Grade
22 questions
Rock-paper-scissors (microbit)
Presentation
•
9th Grade
18 questions
Sistem Komputer
Presentation
•
10th Grade
16 questions
Revising Sentences
Presentation
•
10th Grade
18 questions
Hak Atas Kekayaan Intelektual (HAKI)
Presentation
•
10th Grade
20 questions
Online Essentials - Revision Lesson 1
Presentation
•
10th Grade
17 questions
Module 1 Lesson 2: Input, Output and Storage Devices
Presentation
•
10th Grade
Popular Resources on Wayground
10 questions
HCS SCI 03 Summer School Assessment 1
Quiz
•
3rd Grade
15 questions
HCS SCI 05 Summer School Assessment 1 Review
Quiz
•
5th Grade
22 questions
Day 9 Equations and Inequalities Review
Quiz
•
9th Grade
10 questions
Writing and Identifying Ratios Practice
Quiz
•
5th - 6th Grade
7 questions
PYRAMID PERSPECTIVES part 1
Presentation
•
9th - 12th Grade
12 questions
Understanding the Fourth of July
Quiz
•
9th Grade
15 questions
Soccer World Cup Quiz Questions
Quiz
•
7th Grade