function in python

Functions in Python

Table of Contents

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

“””docstring”””

statement

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

>>>greet(‘Rakul’)

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!”)

greet(‘Rakul’)

Docstrings

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

my_func()

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)

print(absolute_number)

# Output: 20

abs() syntax

the abs() syntax method is

abs(num)

abs()parameters

abs() method takes a single argument:

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

 integer

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

output

Enter a number: 2.4

Enter another number: 6.5

The sum is 8.9

Questions

  1. What are Python functions  and its declaration with an example?
  2. Explain the types of functions in Python with an example?
Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training