Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Functions in Python

A function is a reusable piece of code that can be used any number of times in the program. By using a function, we do not have to write the same code again and again. Instead, we just use the function. For example, you have a lengthy calculation that you have to perform various times in your program. Functions reduce the overhead to a great extent!
Advantages of using functions -
  1. Makes the code reusable.
  2. Dividing the program into functions increases the readability.
  3. Makes it easy for the programmer. No need to write the same code again and again.
  4. Makes the program short.
  5. Makes the program clean and easy to debug.
Like in real life, we perform many functions such as doing homework, reading a book or watching movies etc. In programming, we can make anything a function. For example - a function for adding two numbers or just printing Hello World on the screen. We can make a function as simple or as complicated as we want.

Creating a function

A function has three components -
  1. Return values- In Python, a function can return any value like int, float, etc. It can also return a list, tuple, dictionary, etc. It can return zero, one, or multiple values.
  2. Function Name- It is the name we give to our function so that we can use it later. Like we give names to our variables. A function name should not be a a reserved keyword like pass, break, int, list, etc.
  3. Arguments- It refers to the variables we pass to the function during function call . We can pass any number of arguments to a function.
In Python, a function can be created by using the def keyword. The syntax for creating a function is as follows :

def function_name(parameters):
    # Statements
    
For example - The hello function prints a hello message. It does not return any value.

def hello():
    print("hello from inside a function")
    

Calling a function

Unlike normal statements, functions are not executed automatically. To use a function, we must call it.
For example - To call the hello function, we will write hello(). This will print the hello message.

def hello():
    print("hello from inside a function")

def sum(a,b):
    return a+b

def square_and_cube(a):
    square=a*a
    cube=square*a
    return square, cube

print("Calling hello function")
hello()

print()

print("Calling sum function")
print("To find the sum of 5 and 6, we will write sum(5,6)")
print("Sum of 5 and 6 = ",sum(5,6))

print()

print("We can call a function multiple times")
print("Sum of 2 and 3 = ",sum(2,3))

print()

a=4
print("Calling square_and_cube function")
print("The square_and_cube function returns two values")

square_of_a, cube_of_a = square_and_cube(a)

print("Square of ",a," = ",square_of_a)
print("Cube of ",a," = ",cube_of_a)
Output
Calling hello function
hello from inside a function

Calling sum function
To find the sum of 5 and 6, we will write sum(5,6)
Sum of 5 and 6 =  11

We can call a function multiple times
Sum of 2 and 3 =  5

Calling square_and_cube function
The square_and_cube function returns two values
Square of  4  =  16
Cube of  4  =  64