Functions Flashcards
Creating Functions
Allows you to give a set of instructions a name, so you can trigger it multiple times without having to re-write or copy-paste it.
The contents of the function must be indented to signal that it’s inside.
def my_function():
print(“Hello”)
name = input(“Your name:”)
print(“Hello”)
Calling Functions
You activate a function by calling it.
This is done by writing the name of the function followed by set of round brackets, also allowing you to trigger how many times.
my_function()
my_function()
Functions with Inputs
You can give the function an input, this way the function can do something different depending on the input.
def add(n1, n2):
print (n1 + n2)
add(2, 3)
Functions with Outputs
In addition to Inputs, a function can also have an output. The output value is proceeded by the keyword “return”.
This allows you to store the result from a function.
def add(n1, n2):
return n1 + n2
result = add(2, 3)