Defining a function in python

We should define a function when we need to do something repeatedly – something that can’t be done in a while loop or a for loop.

Below is a sample function which prints a line on standard output.

def print_something():    
    print("Hi there")
print_something()

Save the code in a file func.py and run as

> python func.py
Hi there

But, the above function is pretty boring. It does the same thing again and again. Lets change it. The below function takes an parameter and prints the same.

def print_something(name):    
    print(f"Hi there, {name}")

print_something("Sreedhar")
print_something("Mike")

The output will look as below:

> python func.py
Hi there, Sreedhar
Hi there, Mike