Python: Function

Function definition starts with the “def” keyword.
After “def” comes the function name. We can use any valid function name here.
After the function name, we need parenthesis.
At the end of the definition line, we put a colon(:).
Then we write the definition of the function. All the code that needs to be executed for that functionality should be here.
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()
Python
After the function name, we have the parameters wrapped inside the parenthesis. Parameters are optional, so in case there is no parameter, we can leave the parenthesis blank.
def 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)
Python
We can also define a return value at the end of the function.
If there is a return type then we can get that from the function call, and save it in a variable.
def 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)
Python

Examples

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()
Python

Output:

running function bigBoxFun
Plaintext

Required Parameters

def sum(num1, num2):
    return num1 + num2

result = sum()
print("Result of the sum: ",  result)
Python

Output:

Traceback (most recent call last):
  File "bigboxcode.py", line 11, in <module>
    result = sum()
             ^^^^^
TypeError: sum() missing 2 required positional arguments: 'num1' and 'num2'
Plaintext

Optional Parameters

def bigBoxNumberPrinter(limit=5):
    for i in range(limit):
        print(i)
        
bigBoxNumberPrinter(2)
bigBoxNumberPrinter()
Python

Output:

0
1

0
1
2
3
4
Plaintext

Return 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-

When a function does not return a value we call it a “void” function. (we also call it a “not fruitful” function).
When a function returns a value we call it a “fruitful” function.

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)
Python

Output:

Result of the myCustomFun(20, 50):  20
Result of the myCustomFun(200, 50):  50
Result of the myCustomFun(200, 500):  700
Plaintext

Case #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))
Python

Output:

Result of the myCustomFun():  None
Type of the result:  <class 'NoneType'>
Plaintext

Case #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))
Python

Output:

Result of the myCustomFun():  None
Type of the result:  <class 'NoneType'>
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.