Parameters
In a subroutine, the
()
are for the argument (FYI argument is another word for parameter). These are the pieces of information we pass to the code. These can be variable names that are made up for the first time within the argument ()
.EXAMPLE :
def whichCake(ingredient):
if ingredient == "chocolate":
print("Mmm, chocolate cake is amazing")
elif ingredient == "broccoli":
print("You what mate?!")
else:
print("Yeah, that's great I suppose...")
How do we call the subroutine?
We call it in the same way as before. However, instead of leaving the
()
blank, we send the code a message.EXAMPLE :
def whichCake("chocolate"):
if ingredient == "chocolate":
print("Mmm, chocolate cake is amazing")
elif ingredient == "broccoli":
print("You what mate?!")
else:
print("Yeah, that's great I suppose...")
Adding More Arguments
We can have as many arguments as we want, separated by commas.
EXAMPLE :
def whichCake(ingredient, base, coating):
if ingredient == "chocolate":
print("Mmm, chocolate cake is amazing")
elif ingredient == "broccoli":
print("You what mate?!")
else:
print("Yeah, that's great I suppose...")
print("So you want a", ingredient, "cake on a", base, "base with", coating, "on top?")
whichCake("carrot", "biscuit", "icing")
Comments
Post a Comment