RSPP En-Us SG M05 Functions
RSPP En-Us SG M05 Functions
Name of presenter
Date
© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Welcome to Functions.
What you will learn
3
Functions, continued
Examples:
• print()
• open()
• sum()
• dict()
• and others
4
Types of functions
Takes argument or
No argument taken,
arguments,
no return given
no return given
Types of Python
functions
Takes argument or
No argument taken,
arguments,
returns something
returns something
(fruitful)
(fruitful)
5
Example function 1
• What is the identifier (name of this function)?
• What is the argument?
• What is it returning?
• Is this function fruitful or non-fruitful?
6
Example function 2
• Is this function fruitful or non-fruitful?
• When you run this function, what do you expect to
happen?
• What might be some of the advantages of using an
argument in a function?
7
Organizing code with functions
The input function prompts a message and waits for the user to enter a value ( 5 in
this example )
This value is converted into an integer by the int(…) function and stored in the
variable r
The function calculate_area_circle is called with r as a parameter. It returns the area
of a circle for a radius of value r
The value returned by calculate_area_circle is stored in the variable area
The print function displays the value of the variable area in the console
1. Write a simple function.
Demonstration: Use
2. Run a sample program.
Functions to Organize and 3. Review the results.
Reuse Code 4. Define and use functions without arguments.
5. Define and use functions with arguments.
6. Compare outputs.
Sample code:
Items 1, 2, 3, & 4
def greet_user():
print("Hello there!")
greet_user()
Item 5
def greet_user(name):
print("Hello " + name)
greet_user("Sam")
Checkpoint questions
10
Answers:
1. Function arguments enable developers to pass values to a function. For example,
a function that is called setColor could include a string for the color that is being
passed. Such a call would appear as setColor(“red”).
2. False.
3. You use the name of the function and then its argument in parentheses (which
might be empty). For example: setColor(“red”), clearScreen()
4. Functions enable developers to use the same code many times without retyping
the statements.
5. One of the most commonly used built-in functions is print.
• Functions are used when you must perform the same
Key takeaways task multiple times in a program.
• Functions are called by name and the function call
often includes arguments that the function code
needs for processing.
• Python includes many built-in functions, such as
print and help.
© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
11
• Functions are used when you must perform the same task multiple times in a
program.
• Functions are called by name and often include arguments that the code in the
function needs for processing.