Python Functions

Python Functions

Introduction to python functions and subprograms

ยท

4 min read

Functions are a block of code that performs a specific task. They allow us to split our program into reusable parts.

We can define a function in Python using the def keyword. A basic function definition has the following components:

  • def: Indicates that a function is being defined

  • function_name(): The name of the function

  • Parameters (optional): The parameters passed to the function enclosed in parentheses ()

  • colon :

  • Indented Block: The code block with the statements to be executed.

For example:

def add(x, y):
    return x + y

Here:

  • def indicates we are defining a function

  • add() is the function name

  • (x, y) are the parameters

  • The indented block contains the return statement to return the sum of x and y

We can call the function like this:

result = add(2,3)
print(result)
# Prints 5

Scope

In Python, variables can have either global or local scope.

Global scope:

  • Variables declared outside of a function are known as global variables and have global scope.

  • They can be accessed inside and outside of functions.

Local scope:

  • Variables declared inside a function are local to that function.

  • They can only be accessed within that function.

For example:

x = "global" # global

def func():
    y = "local" # local

    print(x) # Can access global x
    print(y) # Can only access local y

func()
print(x) # Can access global x
# print(y) # Will give an error as y is local to func()

Output:

global  
local
global

To use a global variable inside a function, we need to declare it as global. For example:

x = "global"

def func():
    global x   # Declaring x as global
    x = "local"

    print(x)

func()
print(x)

Output:

local  
local

So in summary, global variables can be accessed inside and outside functions while local variables are only accessible within the function in which they are declared.


Global & Nonlocal

The global and nonlocal keywords in Python are used to modify variables outside of the current scope.

global - It is used to modify a variable from the global scope within a function.

For example:

x = "global"

def func():
    global x  
    x = "modified"

func()

print(x) # prints modified

Here we use global x to modify the global variable x from within the function.

nonlocal - It is used to modify a variable from an enclosing scope within a nested function.

For example:

def outer():
    x = "outer"

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

    inner()
    print(x) # prints inner

outer()

Here we use nonlocal x to modify the variable x from the enclosing outer() scope within the inner() function.

Without nonlocal, inner() would create a new local variable x and modify that, leaving the outer x unchanged.

So in summary:

  • global modifies a variable from the global scope

  • nonlocal modifies a variable from an enclosing scope (not the global scope)

Both allow us to modify variables outside of the current local scope from within a function.


Shadowing

Shadowing refers to a situation where a local variable shares the same name as a global variable, hiding or shadowing the global variable.

For example:

x = "global"

def func():
    x = "local"
    print(x) # Prints local

func()  
print(x) # Prints global

Here, the local x inside func() shadows the global x, hiding it. The local x takes precedence and is printed.

After func() returns, the global x is accessible again and is printed.

So in summary:

  • When a local variable shares the same name as a global variable, the local variable shadows the global one.

  • Within the scope of the local variable, the global variable is hidden.

  • After the local scope ends, the global variable is accessible again.

This is known as the shadowing effect.

We can avoid shadowing by:

  • Using different variable names

  • Using the global keyword to modify the global variable from within the local scope.

For example:

x = "global"

def func():
    global x
    x = "local"
    print(x) # Prints local

func()  
print(x) # Also prints local

Here we use global x to modify the global x from within the local scope, avoiding shadowing.


Disclaim: This article is created with Rix AI. I ask the questions so you don't have to. Learn and prosper. ๐Ÿ––

ย