Functions
# Block of code that only run when they are called
# Define a function
def function1():
print("Hello from function1")
# call a function
function1()
# Function returns a value
def function2():
return "Hello from function2"
return_from_function2 = function2()
print(return_from_function2)
# Pass variable to function
def function3(s):
print("\t{}".format(s))
function("parameter")
# Pass two parameters to function
def function4(s1, s2):
print("{} {}".format(s1,s2))
# Call function
function4("any", "thing")
# Calling it with parameters name means the order doesn't matter
function4(s2="thing", s1="any")
# Defining a function with default parameter
def function5(s1 = "default"):
print("{}".format(s1))
# If no parameter is passed it will use default value
function5()
# Function that handles unknown
def function(s1, *more)
print("{} {}".format(s1, " ".join([s for s in more])))
function6("function6", "a")
# Receive a dictionary of arguments
def function6(**ks):
for a in ks:
print(a, ks[a])
function7(a="1", b="2", c="3")
# Recursive function
function12(x):
print(x)
if x > 0:
function12(x-1)
function12(5)
Last updated