2017-07-18 04:49:56 +00:00
|
|
|
# Simple function
|
|
|
|
def functionName():
|
2020-03-01 00:21:26 +00:00
|
|
|
return True
|
2017-07-18 04:49:56 +00:00
|
|
|
|
2018-07-12 12:57:23 +00:00
|
|
|
# Function with parameters
|
2020-03-01 00:21:26 +00:00
|
|
|
def functionName(a, b):
|
|
|
|
if a < b:
|
|
|
|
return a
|
|
|
|
else:
|
|
|
|
return b
|
2017-07-18 04:49:56 +00:00
|
|
|
|
2018-07-12 12:57:23 +00:00
|
|
|
# Return multiple values
|
2020-03-01 00:21:26 +00:00
|
|
|
def functionName(a, b, c):
|
|
|
|
return a, b, c # Returns a tuple
|
|
|
|
return {'return_a':a, 'return_b':b ,'return_c':c } # Returns a dictionary
|
2018-07-12 12:57:23 +00:00
|
|
|
|
|
|
|
# Function with default parameters
|
2020-03-01 00:21:26 +00:00
|
|
|
def functionName(a=0, b=1):
|
|
|
|
print(a, b)
|
|
|
|
functionName() # 0 1
|
|
|
|
functionName(3) # 3 1
|
|
|
|
functionName(3, 4) # 3 4
|
2018-07-12 12:57:23 +00:00
|
|
|
|
|
|
|
# Calling parameters by name
|
2020-03-01 00:21:26 +00:00
|
|
|
def functionName(a, b, c):
|
|
|
|
print(a, b, c)
|
|
|
|
functionName(0, 1, 2) # 0 1 2
|
|
|
|
functionName(a=2, c=3, b=4) # 2 4 3
|
|
|
|
functionName(2, 3, c=4) # 2 3 4
|
2018-07-12 12:57:23 +00:00
|
|
|
|
|
|
|
# Arbitrary number of parameters
|
|
|
|
def functionName(*args):
|
2020-03-01 00:21:26 +00:00
|
|
|
...
|
|
|
|
functionName(*[1, 2]) # Equivalent of functionName(1, 2)
|
|
|
|
functionName(*[1, 2, 3]) # Equivalent of functionName(1, 2, 3)
|
2018-07-12 12:57:23 +00:00
|
|
|
|
|
|
|
# Arbitrary number of parameters with arbitrary name
|
|
|
|
def functionName(**kwargs):
|
2020-03-01 00:21:26 +00:00
|
|
|
...
|
|
|
|
functionName(**{'a' : 3, 'b' : 4}) # Equivalent of functionName(a=3, b=4)
|