All IT Courses 50% Off
Python Tutorials

Functions in Python

The function in python programming is called a group of that is linked statements that do some specific task. Functions will help to decompose the program into smaller and modular chunks. As the program grows large and larger, the function is going to make more systematic and manageable. It always avoids repetition and makes the code reusable.

The syntax  of function is

     def function_name(parameters):



As the above example states that a function consists of the following components

  1. Keyword def will make a start  of the function header
  2. As the function name to uniquely identify the function. The function name follows the same rules of writing identifiers in python.
  3. The parameters or may be the arguments through which we pass values to a function. which are optional
  4. A colon : is to tarck the end of the function header
  5. Optional documentation is a string which explains what the function does
  6. A valid python statements which has the function body statements and has the statements that must have the similar indentation level.
  7. An optional will return statement to return a value from the function.

def greet(name):


     This function greets to the person passed in as a parameter  “””

     print(“HI, ” + name + “. Good Evening!”)

How we should call the python function?

When we have defined the function we can call it from another function, program, or maybe even a python prompt. To call  a function name with proper parameters like


HI Rakul.Good Evening!

When we try executing the program with the function to see the output.

ef greet(name):


     This function greets to the person passed in as a parameter “””

    print(“HI, ” + name + “. Good Evening!”)



The first string after the function header is called the docstring and is short for documentation string. The documentation is a good programming practice. Consider the example where we have docstring immediately below the function header. This string will be available to us as the _doc_ attribute of the function.

>>> print(greet.__doc__)

    This function greets to the person passed in as a parameter.

How does the function work?

How function works in Python?

Scope and lifetime of variables:

Scope variables are a part of a program where the variable will be recognized. The parameters and variables defined inside a function are not visible from outside the function. The lifetime will be variable throughout which is a variable that exists in the memory. The lifetime inside a function will be not as long as the function executes. An example is shown below

def my_func():

x = 10

print(“Value inside function:”,x)

x = 30


print(“Value outside function:”,x)

Value inside function: 10

Value outside function: 30

The value of x is 30. Where the function my_func() modifies the actual value of x to 10 it will not affect the value outside the function. It is because the variable x inside the function is way different from the one outside. They have the same names where they will be having two different variables with different scopes.

Types of functions

  1. Built-in Functions-Functions that are created to python.

Python_abs() will return value absolute  number.

Python_abs()-An abs() function gives the  actual or absolute value of the given number.If that may be complicated number abs() returns its magnitude.For example

number = -20

absolute_number = abs(number)


# Output: 20

abs() syntax

the abs() syntax method is



abs() method takes a single argument:

  • num-a number whose absolute value is to be returned.The number will be 


floating number

complex number

  1. User-defined functions

Functions to which we will be defining ourselves to do certain specific tasks are referred to as user-defined functions. The way in which we define and call function in python is already told.

Advantages are

  1. user-defined functions are supports to break big program to small pieces of segments that makes program quiet simple to understand.
  2. It is repeated code has in a program function that can be used to include those codes and execute when needed by calling that function.

Consider an example

Program to illustrate

# the use of user-defined functions

def add_numbers(x,y):

   sum = x + y

   return sum

num1 = 5

num2 = 6

print(“The sum is”, add_numbers(num1, num2))


Enter a number: 2.4

Enter another number: 6.5

The sum is 8.9


  1. What are Python functions  and its declaration with an example?
  2. Explain the types of functions in Python with an example?
Facebook Comments

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.