- The subroutines on the other page are simply procedures that run the code within then when called. The other type is a function that returns value(s) rather than printing to the screen
#Functions first
def double(number):
doubled = number * 2
return doubled
#Execute function and print returned value
print(double(10))
#Call function, pass a value (10) and save return value in doubled variable
doubled = double(10)
print(double)
#Use in IF statements
x = 25
if double(x) > 100:
print("It's a big one!")
Tasks
- Type in the code and make sure it works.
- Create a function that takes the radius of a circle, and returns the area of a circle πr2
- Save and label your code
Extension Task
Functions can return more than one value, and can do so at any point using the return command. Create the code to call this function, and modify the code to accept input from the user
#Function
def pos_or_neg(x):
if x > 0:
return "Positive"
elif x < 0:
return "Negative"
else:
return "0"
#Call the function here
#??