Python User Defined Functions - Notes
Python User Defined Functions - Notes
Topics Covered
● What are user-defined functions in Python?
● Advantages of user-defined functions
● Global Variables
● Local Variables
● Global and local variables
● Nonlocal Variables
1. def:
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:
Example Code:
# Program to illustrate
# the use of user-defined functions
def discount(x,y):
final_price = x*(1-y/100)
return final_price
Output:
Enter price : 1000
Enter discount in % : 50
Final price : 500.0
2
Advantages of user-defined functions:
Global Variables:
def foo():
print("x inside:", x)
foo()
print("x outside:", x)
Run Code
Output
x inside: global
x outside: global
x = "global"
3
def foo():
x=x*2
print(x)
foo()
Run Code
Output
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.
foo()
print(y)
Run Code
Output
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.
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().
Here, we will show how to use global variables and local variables in the same code.
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.
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.
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().