def yourFunctionNameHere():
# code for execution
# Whatever functionality we want to implement shold be here
# Indentation before the code(for full section) is required
# Call the function like below
yourFunctionNameHere()
Pythondef yourFunctionNameHere(param1, param2):
# code for execution
# Whatever functionality we want to implement shold be here
# Indentation before the code(for full section) is required
# Call the function like below
yourFunctionNameHere(100, 200)
Pythondef yourFunctionNameHere(param1, param2):
# code for execution
# Whatever functionality we want to implement shold be here
# Indentation before the code(for full section) is required
return param1 + param2
# Call the function like below
result = yourFunctionNameHere(100, 200)
PythonExamples
Let’s start with a simple function definition and usage-
# Define a function
def bigBoxFun():
print("running function bigBoxFun")
# Call and execute the function
bigBoxFun()
PythonOutput:
running function bigBoxFun
PlaintextRequired Parameters
def sum(num1, num2):
return num1 + num2
result = sum()
print("Result of the sum: ", result)
PythonOutput:
Traceback (most recent call last):
File "bigboxcode.py", line 11, in <module>
result = sum()
^^^^^
TypeError: sum() missing 2 required positional arguments: 'num1' and 'num2'
PlaintextOptional Parameters
def bigBoxNumberPrinter(limit=5):
for i in range(limit):
print(i)
bigBoxNumberPrinter(2)
bigBoxNumberPrinter()
PythonOutput:
0
1
0
1
2
3
4
PlaintextReturn Value
We can return some value at any point of the function. The return value can be received by assigning the function call to a variable.
WARNING
The function immediately stops executing further, when it reaches a return statement.
Depending on our implementation a function may or may not return any value-
Case #1: General return Statement
def myCustomFun(num1, num2):
if num1 < 100:
return num1
if num2 < 100:
return num2
return num1 + num2
result = myCustomFun(20, 50)
print("Result of the myCustomFun(20, 50): ", result)
result = myCustomFun(200, 50)
print("Result of the myCustomFun(200, 50): ", result)
result = myCustomFun(200, 500)
print("Result of the myCustomFun(200, 500): ", result)
PythonOutput:
Result of the myCustomFun(20, 50): 20
Result of the myCustomFun(200, 50): 50
Result of the myCustomFun(200, 500): 700
PlaintextCase #2: Empty return Statement
We can just type “return” as a return value, in that case, it will return “None”. Check the code below-
def myCustomFun():
# Perform some operations
# Just use return without any value
return
result = myCustomFun()
print("Result of the myCustomFun(): ", result)
print("Type of the result: ", type(result))
PythonOutput:
Result of the myCustomFun(): None
Type of the result: <class 'NoneType'>
PlaintextCase #3: No return Statement
A function can have no return statement, when we don’t need to return anything. In that case we will receive a ‘None’ if we try to get the result of the function-
def myCustomFun():
# Perform some operations
print("inside myCustomfunc")
result = myCustomFun()
print("Result of the myCustomFun(): ", result)
print("Type of the result: ", type(result))
PythonOutput:
Result of the myCustomFun(): None
Type of the result: <class 'NoneType'>
Plaintext