Lecture # 7 - Functions

Lecture # 7 - Functions

Functions in Python.

Functions:

In Python, a function is a block of reusable code that performs a specific task. Functions provide a way to organize and modularize code, making it easier to understand, reuse, and maintain. Functions can take inputs (arguments) and optionally return an output (result).

Syntax:

def name_of_function(argument):
    """Function Descriptiom"""
    #CODE
    return 
#Function Calling
name_of_function

Example 1:

def hello():
    """Greeting the world"""
    print("Hello, World")
#Function Calling
hello()

This function is not returning anything and this function has no arguments.

Output:

Example 2:

def greeting(name):
    """Greeting the user"""
    print("Hello", name)
#Function Calling
greeting("Abdullah")

This function has an argument but it is not returning anything.

Output:

Example 3:

def sum(a, b):
    """Sum of two number"""
    return a + b
print("Sum =", sum(2, 2))

This function has two arguments and also it is returning a value.

Output:

Example 4:

def square(a, e = 2):
    """Sum of two number"""
    return a ** e
print("Square of a =", square(6))

This function is used to print the square of a number. In this function there are two arguments, a and e. e is defined as 2 in the start. in the return a ** e statement, ** is an operator used for exponential. When we call the function we will have to only give value for a because e is pre defined.

Output:

Scope:

A variable is only accessible within its scope. There are two types of scope:

  1. Global Scope:

Variables created outside any scope are accessible globally in the whole program. Such variables have the global scope.

Example:

name = "Abdullah"
def show_name():
    """Display Name"""
    print("My name is", name)
show_name()

In the above code name is a variable that has the global scope because it is not created in any scope. This variable can be accessed by any function or any scope as we accessed in the function named show_name .

Output:

  1. Local Scope:

Variables created inside the functions or any scope have local scope. These variables re only accessible within the function or scope.

Example:

name = "Abdullah"
def name_and_age(age):
    """Display Name and Age"""
    print("My name is " + name + " and I am " + str(age) + " years old.")
name_and_age(20)

In the above code name is a variable that has global scope and age is a variable that has local scope only means that the variable age can only be accessed inside the name_and_age function.

Output:

Now, if I try to print name it'll be shown but if I try to print age outside the scope there will be an error. The code will be:

name = "Abdullah"
def name_and_age(age):
    """Display Name and Age"""
    print("My name is " + name + " and I am " + str(age) + " years old.")
name_and_age(20)
print(name)
print(age)

The console will show the error and print the output of the correct part of the code:

See the variable name is accessed because it has the global scope.