To begin grouping related code, we start defining a function with the keyword def
.
def
Next comes the function's name in snake case, like greet_user
, followed by parentheses, (
and )
.
def greet_user()
Like before, a colon :
, marks the beginning of the code block that belongs to the function.
def greet_user():
To group code into the function, we indent it by two spaces. This function groups two related print()
statements.
def greet_user():
print("Good morning Anna")
print("Welcome back")
To run the code, we need to call the function. We do that by coding its name followed by parentheses, like greet_user()
.
def greet_user():
print("Good morning Anna")
print("Welcome back")
greet_user()