Functions are a convenient way to group our code into reusable blocks.
In Python defining a function looks something like this.
def function_name():
#function tasks go here
- def keyword indicates the beginning of a function.
- Following the function name is parenthesis ( ) that can hold input values.
- A colon : marks the end of the function
- Similar to loops and conditions in python, code inside a function must be indented to indicate that they are part of the function.
Calling a function
Now, to call a function you must type out the functions name followed by ( ) parentheses. This does not need to me indented.
function_name()
Parameters & Arguments
Function Parameters allow our function to take in data as an input value.
Heres an example
def welcome_guest(name):
print("Welcome to our Airbnb!")
print("We are excited to have you come stay with us" + name)
We would use our parameter by passing an argument to the function like this
welcome_guest("Sam")
This would output:
Welcome to our Airbnb!
We are excited to have you come stay with us Sam.
Here is a visualization of the distinction between a parameter and an argument
Top comments (0)