[go: up one dir, main page]

0% found this document useful (0 votes)
3 views24 pages

Functions

The document provides an overview of functions in Python, detailing their definitions, types (built-in, user-defined, and anonymous), and how to define and call them. It explains the concepts of parameters vs. arguments, namespaces, scope, and the differences between call by value and call by reference, particularly in relation to mutable and immutable objects. Additionally, it covers the use of the global keyword for modifying global variables within local contexts.

Uploaded by

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

Functions

The document provides an overview of functions in Python, detailing their definitions, types (built-in, user-defined, and anonymous), and how to define and call them. It explains the concepts of parameters vs. arguments, namespaces, scope, and the differences between call by value and call by reference, particularly in relation to mutable and immutable objects. Additionally, it covers the use of the global keyword for modifying global variables within local contexts.

Uploaded by

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

FUNCTIONS

Laxmi P
PGT-CS
KV Kokrajhar
Functions

■ Function is a piece of code written to carry out a specific task


■ Function may or maynot need multiple inputs
■ A function can or cannot return one or more values
■ Three types of functions:
– Built-in functions –Functions which are part of Python installation
etc:len,min,max,help etc
– User-Defined Functions –Functions created by the user for their
requirement
– Anonymous functions- lambda functions as it doesn’t use the def
keyword for function creation
Defining a function in Python

■ Use the keyword def to declare the


function and follow this up with the
function name
■ Add parameters to the function: they
should be within the parentheses of the
function. End your line with a colon.
■ Add statements that the functions
should execute.
■ End your function with a return
statement if the function should output
something. Without the return
statement, your function will return an
object None
Calling a function

■ To call a function mention the function name followed by arguments in


parenthesis

■ Output is obtained
Flow of Execution in Function call

■ Execution flow refers to the order in which statements


are run by the interpreter
■ A block is executed in an execution frame
■ When an interpreter sees a function definition it just
reads it and remembers. It doesn’t execute the code.
■ Only when a function call is made the statements inside
a function is executed
■ Execution frame is created when a function call is
encountered
■ And its deleted when RETURN/last statement is
encountered
Parameters vs Arguments

■ Parameters are the names used when defining a function or a


method, and into which arguments will be mapped.
■ Arguments are the things which are supplied to any function or
method call, while the function or method code refers to the
arguments by their parameter names.
Parameters

Arguments
Passing parameters to functions
■ Positional / Required arguments : All the arguments need to be passed
in function call. We cannot skip any arguments.
■ Default arguments : Parameter having default value in function
header is known as default parameter
– These types of parameters need not be passed during function
call.( if a value is passed for default parameter in function call – it
is overwritten)

■ Keyword arguments( Named): named arguments can be provided in


function call
Contd..

■ Default arguments cannot come before required arguments


■ Positional arguments has to be passed before key word arguments
def interest(prin,time,rate=.10) Legal

def interest(prin,time=2,rate=.10) Legal

def interest(prin=1500,time,rate=.10) Illegal

def interest(prin,time=2,rate) Illegal

def interest(prin=1500,time=2,rate=.10) Legal

interest(prin,time,rate=.10) Legal
interest(prin,time=2,rate=.10) Legal
interest(prin=1500,3,rate=.10) Illegal
interest(prin,time=2,rate) Illegal
interest(prin=1500,time=2,rate=.10) Legal
Namespaces in Python

■ A Namespace is a mapping from names to objects


■ A namespace is implemented in Python as a dictionary
■ A Scenario you can relate with to understand namespace
A name Alice can exist for more than one student, If Class XI and XII
have students by the name Alice each then Class XI and Class XII are the
name spaces
■ There is no relation between names in different namespaces
■ Examples of namespaces are: the set of built-in names (containing
functions such as abs(), and built-in exception names); the global
names in a module; and the local names in a function invocation.
Namespaces contd..
■ Namespaces are created at different moments and
have different lifetimes
■ The namespace containing the built-in names is
created when the Python interpreter starts up, and is
never deleted.
■ The global namespace for a module is created when
the module definition is read in
■ The statements executed by the top-level invocation of
the interpreter, either read from a script file or
interactively, are considered part of a module called
__main__, so they have their own global namespace
■ The local namespace for a function is created when
the function is called, and deleted when the function
returns or raises an exception that is not handled
within the function.
Scope in Python
■ Textual region of a python program where a namespace is accessible
■ Scopes are determined statically but they are used dynamically
■ At any time during execution, there are at least three nested scopes
whose namespaces are directly accessible:
– the innermost scope, which is searched first, contains the local
names
– the scopes of any enclosing functions, which are searched starting
with the nearest enclosing scope, contains non-local, but also non-
global names
– the next-to-last scope contains the current module’s global names
– the outermost scope (searched last) is the namespace containing
built-in names
Example-Scope
■ some_func is called from global
namespace
■ Some_func has a nested function
some_inner_func which has a var 10
declared.
■ some_inner_function is called which
Creates the variable var with value 10
■ var is declared inside nested function.
The scope of the variable is only in the
nested local namespace
■ Printing var inside gives the correct
value 10
■ The function ends and execution
begins at line no.8
■ Its trying to print var but var is not in
local namespace or global namespace
Function invocation-Call by
Value/Call by reference
■ Call by value method copies the value of an argument into the formal
parameter of that function
– Therefore, changes made to the parameter of the main function do
not affect the argument.
– The actual parameters and functions parameters reside in
different memory locations
■ Call by reference method copies the address of an argument into the
formal parameter.
– In this method, the address is used to access the actual argument
used in the function call
– It means that changes made in the parameter alter the passing
argument.
Contd..

■ Call by Value

■ Call by Reference
Python-Call-by-Object
■ Python Passes arguments by Call-by-Object
■ Immutable arguments like integers, strings or tuples to a function if
passed, the passing acts like call-by-value.
■ They can't be changed within the function, because they can't be
changed at all, i.e. they are immutable
■ Mutable arguments are also passed by object reference, but they
can be changed in place in the function.
Passing Immutable objects
(Strings,integers,tuples)
■ String

■ sent_text is a immutable string datatype


■ The value in sent_text is passed onto
function
■ Initially sent_text and text will point to same
object “hello”
■ But when the value of text is changed inside
function a new object with the new text is
created and referred to
■ Any change for the value text inside function
will not affect sent_text
Integer
■ Vara with value 10 is passed

■ Varb and vara is pointing to same


object initially
■ Trying to change varb inside function

■ A new object is created with value 30


and varb now points to it
contd
■ We can have same name for both
actual and formal parameter

■ The actual parameter vara is having


value 10
■ Its passed onto function and function
changes it to 30
■ Printing the vara inside function will
print 30 as its in local namespace
■ The vara changed inside function did
not affect vara which is in global
namespace
Tuples
■ tup1 is passed to function

■ Tup2 value changes


■ But it dint affect tup1
Passing Mutable Objects- Lists

■ List behaves differently on 2


occasions
1. Creates separate objects
when updated in this way
Passing Mutable Objects- Lists

2. Links to same object when


called this way

Values in list gets appended to


temp
Contd…
Passing Mutable Objects-
■Dictionary
Dictionary is passed to the
function Global
frame
■ The function adds a new key dict1 Object
value pair to the dictionary
{1:”Ram”,2:”Sam”,3:”J
■ The dictionary in the global ohn”}
space also reflects the change dict_change
■ Both the variables are referring temp
return
to the same dict in memory
Global Keyword
■ Global keyword allows modification of variable outside the current
scope
■ Its used to create global variable and to make changes to the variable
in the local context
■ A variable inside a function is local by default
■ A variable outside a function is global by default

You might also like