Functions in Python

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

Function is divided mainly into 2 parts in python:

FUNCTION DEFINITION IN PYTHON

In Python a function is defined using the def keyword. The syntax of function definition is:

def name():
     print("hello world")

OUTPUT:

DEFINING FUNCTION IN PYTHON
OUTPUT – DEFINING FUNCTION IN PYTHON

FUNCTION CALLING IN PYTHON

In this, we can call a function name which we define it earlier. To call a function, use the function name followed by parenthesis. The syntax is:

def name():
    print("hello world")

name()

After calling a function, it starts performing its task which is assigned to it.

OUTPUT:

FUNCTION CALLING IN PYTHON
OUTPUT – FUNCTION CALLING IN PYTHON

PARAMETERIZED FUNCTION IN PYTHON

In this, we can pass parameters through function. Information can be passed to functions as parameter. Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

The syntax is:

def fname(name):
    print(name + " Sharma")

fname("Divya")
fname("Armaan")

OUTPUT:

FUNCTION IN PYTHON
OUTPUT – FUNCTION IN PYTHON

Leave a Reply

Your email address will not be published. Required fields are marked *