
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
Microsoft Excel
Presentation
•
11th - 12th Grade
16 questions
MATERI 3 KOMPUTER DAN JARINGAN DASAR
Presentation
•
10th Grade
15 questions
bimbingan tik
Presentation
•
10th Grade
18 questions
Untitled Lesson
Presentation
•
11th Grade
18 questions
1.2.2 Malicious Code Part 2
Presentation
•
11th Grade
19 questions
GCSE Computer Science Introduction
Presentation
•
9th - 10th Grade
20 questions
[IGCS] Types of Storage Devices
Presentation
•
9th Grade
17 questions
komponen sistem komputer
Presentation
•
10th Grade
Popular Resources on Wayground
20 questions
STAAR Review Quiz #3
Quiz
•
8th Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
6 questions
Marshmallow Farm Quiz
Quiz
•
2nd - 5th Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
20 questions
Inferences
Quiz
•
4th Grade
19 questions
Classifying Quadrilaterals
Quiz
•
3rd Grade
12 questions
What makes Nebraska's government unique?
Quiz
•
4th - 5th Grade
Discover more resources for Computers
50 questions
STAAR English 2 Review
Quiz
•
10th Grade
20 questions
Figurative Language Review
Quiz
•
10th Grade
20 questions
Grammar
Quiz
•
9th - 12th Grade
31 questions
Easter Trivia
Quiz
•
KG - 12th Grade
16 questions
Circles - Equations, Central & Inscribed Angles
Quiz
•
9th - 12th Grade
46 questions
Unit 4 Geosphere Test Review
Quiz
•
9th - 12th Grade
10 questions
Calculating Surface Area of a Triangular Prism
Interactive video
•
6th - 10th Grade
20 questions
Central Angles and Arc Measures 2
Quiz
•
10th Grade