0% found this document useful (0 votes)
19 views14 pages

FN 1

The document provides an overview of functions in Python, explaining their purpose in organizing code, avoiding repetition, and enhancing reusability. It covers the types of functions (built-in and user-defined), how to create functions, pass values, manage variable scope, and utilize return statements. Additionally, it discusses importing user-defined functions into other programs.

Uploaded by

shroffyohann2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

FN 1

The document provides an overview of functions in Python, explaining their purpose in organizing code, avoiding repetition, and enhancing reusability. It covers the types of functions (built-in and user-defined), how to create functions, pass values, manage variable scope, and utilize return statements. Additionally, it discusses importing user-defined functions into other programs.

Uploaded by

shroffyohann2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Functions

Functions
• A function in Python is a group statements used to perform specific task
when it is called.

• Functions help break our program into smaller and modular chunks. As
our program grows larger and larger, functions make it more organized
and manageable.

• Furthermore, it avoids repetition and makes code reusable.

• Data can be passed into a function called parameters/arguments and


function process the data and return the data as well.
Types of Functions
• Basically there are two types of functions

• Built-in Functions
• User Defined Functions.

• Functions that are readily available to use are called Built-in functions
and there are many available in python for ex, print(), len(),etc..
• Functions that are defined by the user to do certain tasks are called
User defined functions.
Creating a Function
• A Function in Python is defined by the "def " statement followed by the
function name and parentheses. The basic syntax is

def function name(Arguments/Parameters) :


“”” Document string”””
Python statement(s)

Information can be passed into functions as arguments/parameters and


they are optional.
A parameter is the variable listed inside the parentheses in the function
definition.
An argument is the value that is sent to the function when it is called.
A Document string is used to describe what function does and is optional
Creating a Function
def myname():
print("Srinivas")

myname()

Output: Srinivas

def display():
print("Welcome to Python Programming")

display()

Output: Welcome to Python Programming


Passing values to a Function
• Information can be passed as arguments/parameters into a function as
direct value(s).
def num(a): def add(a,b):
print("Number is" ,a) print(“Sum is" ,a+b)

num(5) add(2,3)

Output: Number is 5 Output: Name : Sum is 5


def name(a): def add(a,b):
print("Name :" ,a) print(“Sum is" ,a+b)

name(‘Srinivas’) add(b=2,a=3)

Output: Name : Srinivas Output: Name : Sum is 5


Passing references to a Function
• Information can be passed as arguments/parameters into a function as
variable(s).
def num(a): def mul(a,b):
print("Number is" ,a) print(“Value is" ,a*b)
b=5 p=2;q=3
num(b) mul(p,q)

Output: Number is 5 Output: Name : Value is 6


def name(a): def mul(a,b):
print("Name :" ,a) print(“Value is" ,a*b)
N=‘Srinivas’ p=2;q=3
name(N) mul(p)

Output: Name : Srinivas Output: Error


Passing references to a Function
• Information can be passed as arguments/parameters into a function as
variable(s).
def fun1(L1): def fun1(L1):
L1=[2,3] L1.append(5)
print(L1) print(L1)
L=[1,2,3,4] L=[1,2,3,4]
fun1(L) fun1(L)
print(L)
Output:
[2, 3] Output: Name :
[1, 2, 3, 4] [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
Scope of variables in a Function
• The variables declared inside a function are local to the function i.e. the
life time of the variable is valid only when the function is called and is
limited within the function.
def fun1(a): def fun1(L1):
a=20 a=5
print(a) b=10

a=10 print(a,b)
fun1(a)
print(a) Output: Error

Output:
20
10
Scope of variables in a Function
• We can make a function variable valid even outside the function by
declaring it as a global variable by using the key word ‘global’.

def fun1(a):
global b
b=20
print(a+b)

a=10;b=15
fun1(a)
print(b)

Output:
30
20
Passing variable length arguments
• We can pass arbitrary number of parameters to a function. The output
will be stored in the form of tuples.

def fun1(*num): def fun1(*L1):


print(num) print(L1)
fun1(1,2,3,4) L=[1,2,3]
fun1(L)
Output: L=[1,2,3,4]
(1,2,3,4) fun1(L)

Output:
([1, 2, 3],)
([1, 2, 3, 4],)
Use of Return statement
• The return statement is used to end the execution of the function call
and “returns” the result (value of the expression following
the return keyword) to the caller. The statements after the return
statements are not executed. If the return statement is without any
expression, then the special value None is returned. Return statement
can not be used outside the function. The syntax is

def fun():
statements
..
..
return [expression]
Use of Return statement
def fun1(p):
return p
a=10
fun1(a) #No output
print(fun1(a)) #Returns the function result

def greater(p,q):
if p>q:
return True
else:
return False
a=10
b=20
greater(a,b) #No output
print(greater(a,b)) #Returns the function result
Importing user defined functions
• We can use any user defined functions in other programs by using
‘import’ statement.

def gre(a,b): # Save this as greater.py


if a>b:
print("True")
else:
print("False")

Import greater # Importing the file factorial.py


greater.gre(2,3)

Import greater as greatvalue # Renaming file name


greatvalue.gre(4,3)

You might also like