Functions
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
Creating a Function
Section titled “Creating a Function”In Python, a function is defined using the def keyword:
def my_function(): print("Hello from a function")Calling a Function
Section titled “Calling a Function”To call a function, use the function name followed by parenthesis:
def my_function(): print("Hello from a function")
my_function()Arguments
Section titled “Arguments”Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses.
def greet(name): print("Hello, " + name)
greet("Zavont")greet("Developer")Return Values
Section titled “Return Values”To let a function return a value, use the return statement:
def multiply_by_five(x): return 5 * x
print(multiply_by_five(3))print(multiply_by_five(9))