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

Python User Defined Functions - Notes

Uploaded by

tanwarvaishu9
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)
0 views

Python User Defined Functions - Notes

Uploaded by

tanwarvaishu9
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/ 7

User-Defined Functions

Topics Covered
● What are user-defined functions in Python?
● Advantages of user-defined functions
● Global Variables
● Local Variables
● Global and local variables
● Nonlocal Variables

What are user-defined functions in Python?

Functions that we define ourselves to do certain specific tasks are referred to as


user-defined functions. The functions that are already available in python and can be
called directly are known as built-in functions.
When we want to use the same lines of code again and again in different parts of the
code, it is advisable to create a user-defined function, to optimize the code.

To create a user-defined function in python we use the keyword def.

A sample structure of the user-defined function is:


def function_name(arguement1, arguement2, …):
……..Code lines
:::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::
return variables

While calling the function:


function_name(parameters)

1. def:

keyword for creating a user-defined function

2. function_name:

1
A user-defined name following the same name convention as variables

3. argument1, arguement2,...:

These are arguments and depend on the user, generally the input to the
function

4. return:

A 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.

5. variables:

variables inside the function that we want to return

Example Code:

# Program to illustrate
# the use of user-defined functions

def discount(x,y):
final_price = x*(1-y/100)
return final_price

price = int(input("Enter price : "))


coupon = int(input("Enter discount in % : "))
print("Final price : ",discount(price,coupon))

Output:
Enter price : 1000
Enter discount in % : 50
Final price : 500.0

2
Advantages of user-defined functions:

1. User-defined functions help decompose a large program into small segments,


making code easy to comprehend, maintain and debug.
2. Suppose repeated code occurs in a program. A function can be used to include
those codes and execute them when needed by calling that function.
3. Programmers working on large projects can divide the workload by making
different functions.

Global Variables:

In Python, a variable declared outside of the function or in global scope is known as a


global variable. This means that a global variable can be accessed inside or outside of
the function.

Let's see an example of how a global variable is created in Python.

Create a Global Variable


x = "global"

def foo():
print("x inside:", x)

foo()
print("x outside:", x)
Run Code
Output

x inside: global
x outside: global

What if you want to change the value of x inside a function?

x = "global"

3
def foo():
x=x*2
print(x)

foo()
Run Code
Output

UnboundLocalError: local variable 'x' referenced before assignment

There is an error because x is a local variable and is not defined inside foo().

Local Variables
A variable declared inside the function's body or in the local scope is known as a local
variable.

Accessing local variables outside the scope


def foo():
y = "local"

foo()
print(y)
Run Code
Output

NameError: name 'y' is not defined

The output shows an error because we are trying to access a local variable y in a global
scope whereas the local variable only works inside foo() or local scope.

Let's see an example of how a local variable is created in Python.

Example 3: Create a Local Variable


Normally, we declare a variable inside the function to create a local variable.

4
def foo():
y = "local"
print(y)

foo()
Run Code
Output

local

Let's take a look at the earlier problem where x was a global variable and we wanted to
modify x inside foo().

Global and local variables

Here, we will show how to use global variables and local variables in the same code.

Using Global and Local variables in the same code


x = "global "

def foo():
global x
y = "local"
x=x*2
print(x)
print(y)

foo()
Run Code
Output

global global
local

In the above code, we declare x as a global and y as a local variable in the foo(). Then,
we modify the global variable x and we print both x and y.

5
After calling the foo(), the value of x becomes global because we used the x * 2 to print
two times global. After that, we print the value of local variable y i.e local.

Global variable and Local variable with the same name


x=5

def foo():
x = 10
print("local x:", x)

foo()
print("global x:", x)
Run Code
Output

local x: 10
global x: 5

In the above code, we used the same name x for both the global variable and local
variable. We get a different result when we print the same variable because the variable
is declared in both scopes, i.e. the local scope inside foo() and the global scope outside
foo().

When we print the variable inside foo() it outputs local x: 10. This is called the local
scope of the variable.

Similarly, when we print the variable outside the foo(), it outputs global x: 5. This is
called the global scope of the variable.

Nonlocal Variables
Nonlocal variables are used in nested functions whose local scope is not defined. This
means that the variable can be neither in the local nor the global scope.

Let's see an example of how a nonlocal variable is used in Python.

We use nonlocal keywords to create nonlocal variables.

6
Example 6: Create a nonlocal variable
def outer():
x = "local"

def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)

inner()
print("outer:", x)

outer()
Run Code
Output

inner: nonlocal
outer: nonlocal

In the above code, there is a nested inner() function. We use nonlocal keywords to
create a nonlocal variable. The inner() function is defined in the scope of another
function outer().

You might also like