Python Functions
A function is a block of code that 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
In Python, a function is defined using the def keyword.
def my_function():
print("Hello from a function")
Calling a Function
To call a function, use the function name followed by parenthesis.
my_function()
Parameters
Information can be passed to functions as parameters.
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Return Values
To let a function return a value, use the return statement.
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))