0% found this document useful (0 votes)
9 views

Functions

Uploaded by

poojaluv25
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)
9 views

Functions

Uploaded by

poojaluv25
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

A subprogram that acts on data and often returns a value

▪ Enables reusability and reduces redundancy.


▪ Makes a code modular.
▪ Provides abstraction functionality.
▪ The program becomes easy to understand and manage.
▪ Breaks an extensive program into smaller and simpler
pieces.
▪Built-in functions – eg. print(), len(), int() etc.
▪Functions defined in modules – to use
math.pow(), we need to import math module
▪User-defined functions – Defined by
programmers
▪ Python supports three types of formal arguments/parameters:
Important points to remember about function argument
• Default arguments should follow non-default
arguments
• keyword arguments should follow positional
arguments only.
• The order of keyword arguments is not
important, but All the keyword arguments
passed must match one of the arguments
accepted by the function.
• No argument should receive a value more
than once.
SCOPE OF
VARIABLES
▪ Global variables: are created when the program
begins and are lost when it ends.They are
accessible throughout the entire program, and
inside every function. Global variables are declared
outside of every function.

▪ Local variables: When a variable is defined within a


function's body or a local scope or block, we call
such variables a local variable. These variables are
known to have a local scope. Local scope means that
these variables can be used or accessed within the
function's scope
NAME RESOLUTION
(RESOLVING SCOPE OF A NAME):

▪ The LEGB rule is a name lookup procedure that


determines the order in which Python looks up
names. LEGB stands for Local, Enclosing, Global, and
Built-in.
▪ When you pass a mutable object to a function, the function is actually passed a
reference to the object. This means that the function can change the object's
contents, but it cannot change the object's identity.

▪ When you pass an immutable object to a function, the function is passed a copy of
the object. This means that the function cannot change the object's value.
def change_string(str1):
str1 = "Hello world, Welcome to NPS"

str1 = "Hello, world!"


change_string(str1)
print(str1)

You might also like