bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Functions:

In this module we will be going through the below given content:

Learn Python in 16 hrs from experts



Python Functions:

Function is a block of code which provides the re-usability of code. There are two types of functions –
Built – in function (Already created i.e. predefined)
User defined function (Created by users according to the requirements)

Defining a Function

 

A function contains following format –

def (arg1, arg2,… argN):
return

 

  • def keyword is used to start the Function Definition
  • def is followed by function-name which is followed by parenthesis which contains the arguments passed by the user and use the colon at the end
  • Statement specify the body of function.
  • Return sends a result object back to the caller

Calling function

 

(parameters)

e.g.

def sum(x, y):
z = x + y
return z
sum(5,10)Output
15

Wish to Learn Python? Click Here

Scope of variable

Scope of a variable means the portion of the program where you can access a particular identifier. There are two types of scope of variable –

  • Local Variable
  • Global Variable

Local variables

 

The variables which can only be accessed in that function where it is declared. Outside the function body, it cannot be accessed.

def sum(x, y):
z=x + y
print(z) # print 8
return
sum(2,6)
print(z) # shows errorOutput
8
—————————————————————————
NameError Traceback (most recent call last)
in
4 return
5 sum(2,6)
—-> 6 print(z) # shows errorNameError: name ‘z’ is not defined

Global variables

 

Variable which is declared outside the function is known as global variable. It can be accessed anywhere throughout the program.

x=20
def sum(y):
z=x + y
print(z) # print 30
return z
sum(10)
print (x) # print 20output:
30
20
Become Python Certified in 16 hrs.
CLICK HERE

close

Anonymous Function:

Functions which have no name is known as anonymous function which is created by using lambda. is created without using the def keyword.

Syntax:

lambda arg1,arg2,arg3,…,arg n :expression

e.g.

sum=lambda i1:i1+i1
print “Sum is”,sum(20)
Output
Sum is 40

Now that we are familiar with the functions of python.

We can go ahead a bit deeper and discuss how to deal with the files and perform different operations on them from our Python tutorial.

Getting problem with Python learning? Take our Python certification course with a free guide to crack interview questions.

Previous Next

Download Interview Questions asked by top MNCs in 2018?

Learn SQL in 16 hrs from experts



"0 Responses on Functions in Python"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Functions in Python.