Functions pt 2
CSCI100: Intro to computer science
Announcements and Reminders
A little more on functions
Remember that functions are reusable blocks of code
Return Values
def function_name():# function definition
# some code
return # something or nothing
Let’s look at the addition example we had Monday
Create a function that asks the user to enter two numbers, then adds the two numbers together
If I were to print the function call, it would print None because the function doesn’t return anything. How can I return the sum instead of printing it?
Return Values
Let’s do a little practice
Write a function that takes in a the length and width of a 4-sided polygon.
If it is a square, return the area. If it’s a rectangle, return the perimeter
EX: length: 6, width: 10, result: 32
Now it’s your turn to practice!
Integers, so how can we work around that?
Now it’s your turn to practice!
Scope
Scope
Scope
Scope
See you tomorrow! And continue practicing!