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

Save the code in a file and run as

> python
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}")


The output will look as below:

> python
Hi there, Sreedhar
Hi there, Mike