Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python - Slide 4

Functions in Python

Uploaded by

aditya.2352700
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python - Slide 4

Functions in Python

Uploaded by

aditya.2352700
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Problem Solving using Python

Function

By: Dr. Aditya


Saxena
Functions – introduction
• It is difficult to prepare and maintain a large-scale program and the
identification of the flow of data subsequently gets harder to understand.

• The best way to create a programming application is to divide a big program


into small modules and repeatedly call these modules.

• With the help of functions, an entire program can be divided into small
independent modules (each small module is called a function). This
improves the code’s readability as well as the flow of execution as small
modules can be managed easily.
Function in
Python Formal
arguments
def
function_name ( parameters Documentation
‘’’function_docstrin ): Option
string al
g’’’
Statement/s
return
[expression]
Actual
arguments
function_name( paramet
ers )
Function Example
-1
def fun(a, b):
'''Function to add two values'''
c=a+b
print(c) Output:

Function to add two values


print(fun.__doc__) 30

fun(10, 20)
Function Example
-1
def fun(a, b):
'''Function to add two values'''
c=a+b
return c Output:

Function to add two values


print(fun.__doc__) 30

print(fun(10, 20))
Function Example
-1
def fun(a, b):
'''Function to add two values'''
c=a+b
return c Output:

Function to add two values


print(fun.__doc__) 30

z = fun(10, 20)
print(z)
THE return STATEMENT

The return statement is


used to return a value
from the function. It is
also used to return from
a function, i.e. break
out of the function.
Function
Example - 2
Introduction to
PYTHON
Function Arguments
By: Dr Aditya
Saxena
Function
Arguments
• A function can be called by using the
following types of formal arguments
– Positional (Required) arguments
– Keyword arguments
– Default arguments
– Variable-length arguments
Positional
Arguments
• The arguments passed to a function in
correctpositional order.
• The number of arguments in the function call
should match exactly with the
functiondefinition.
Positional
Arguments
def fun( a, b,
c):
print (a, b,
c)
fun(10, ‘hello’,
10.5)

Output: 10 ‘hello’

10.5
Keyword
Arguments
• The caller identifies the arguments by the parameter name.
• This allows you to:
- skip arguments or
– place them out of order

• Python interpreter is able to use the keywords provided to


match the values with parameters.
Keyword
Arguments
Keyword
Arguments
def fun(x , y ,
z):
print (x , y , z)

fun(x = 10, y = ‘hello’, z


= 10.5) fun(y = ‘hello’, z
= 10.5, x = 10) Output:
10 ‘hello’ 10.5
Default
Arguments
• An argument that assumes a default value if a value is
not provided
in the function call for that argument.

>>>def fun(x = 0 , y =
0 , z= 0 ): print
(x , y , z)
>>>fun(100, 200,
300)
>>>fun(100, 200)
>>>fun(100)
>>>fun()
Default
Arguments
Default
Arguments

Output
Default
Arguments

Output
THE LOCAL AND GLOBAL SCOPE OF A VARIABLE

• Variables and parameters that are initialized within a function, are said
to exist in that function’s local scope. Variables that exist in local
scope are called local variables.

• Variables that are assigned outside functions are said to exist in global
scope. Therefore, variables that exist in global scope are called global
variables.
THE LOCAL AND GLOBAL SCOPE OF A VARIABLE

Global variables are accessible to all functions in their scope.


Introduction to PYTHON
Anonymous Functions / Lambda
Function
By: Dr Aditya Saxena
The Anonymous Functions – Lambda (λ) Function

• No def keyword.

• Use lambda keyword to create anonymous functions

• lambda operator can have any number of arguments


(before :),
• It can have only one expression (after :).

• It cannot contain anystatements

• It returns a function object which can be


assigned to any
variable.
• The syntax of lambda function contains only a
single statement, which is asfollows-

lambda [arg1
[,arg2,.....argn]]:expression
>>>def add(x, y,
z):
return x + y
+z

# Cal the function


>>>add(2, 3, 4)
9
Arguments Expression
Function Object

>>>a = lambda x, y,
z: x+y+z
>>>a(10,2
0,30) 60

You might also like