Skip to main content

Functions in Python



In this part of the tutorial, we are going to discuss about Function in python. Before going into it, Let’s have a quick recap to the Python Basics we had previously completed.

Learn Python from scratch by our Expert instructor Start Now!



When programming, you will encounter the calculation and logical operation you need to perform  repeatedly. One way to handle this is to write the same code over. A better solution is to write a function, function enable you to reuse the logic an infinite number of times without repeating yourself.

Time to begin,

Definition

A function is a block of code which runs in a certain oreder  is called it by name. It can be passed data to operate on  the parameters and can return value. All data that is passed to a function is explicitly passed.

Benefits of Python Function

  •          Reuse of code
  •          Reducing duplication of code
  •          Decomposing complex problems into simpler pieces
  •          Improving clarity of the code
  •          Information hiding

Basic Syntax of Function

def function_name(parameters):

                """docstring"""

                statement(s)

Let see,

How to create a function in python?

A function is defined by using def keyword

Here’s one

To define a function, you write def followed by the name of the function

def my_function(name):

print(“Hi ,”+name+ “ .  Happy Morning”)

How to call a function?

Once we have defined a function, we can call it from another function. To call a function  simply use  the function name with appropriate parameters.

>>> myfunction('Yuvi’)

Hello, Yuvi. Happy morning!

Docstring

The first string after the function header is called the docstring and is short for documentation string. It is used to explain in brief, what a function does.


The  Return Statement

The statement return execute a function and passing back to the  place where it was called

Syntax  : return [expression_list]

Example,

def sum( arg1, arg2 ):

 # Add both the parameters and return them."
   total = arg1 + arg2
   print "Inside the function : ", total
   return total;

# Now you can call sum function
total = sum( 10, 20 );
print "Outside the function : ", total

Output :

Outside the function :  30
Inside the function :  30

Check out :  Installation of Python


Scope of Variable

All variables in a program that cannot be accessible at all the location so it based on where you have declared a variable. There are two basic scopes of  variables in python ,
  •          Local variables
  •          Global variables

Local and Global Variable

Scope of a variable is the portion of a program where the parameters and variables defined inside a function is not visible from outside. Hence, they have a local varaibles.

On the otherhand, Global variables can be accessed through the entire program body by all functions.

def my_func()
x = 10
print("Value inside function:",x)
x = 20
my_func()
print("Value outside function:",x)

Output :

Value inside function: 10
Value outside function: 20






Comments

Post a Comment