Functions & Procedures
Introduction to Functions
What are Functions?
Functions are blocks of code that perform a specific task and return a value. They are a type of subprogram, along with procedures. They are reusable and can be called multiple times within a program. Functions are designed to accomplish a specific objective and are usually named with a verb (e.g., calculateSum, validateEmail).
Functions are used when the result of a calculation or operation needs to be obtained
Function Example
def calculateSquare(num):
return num * num
result = calculateSquare(5)
Which keyword is used to define a function in python?
Introduction to Procedures
What are Procedures?
Procedures, also known as subroutines or methods, are blocks of code that perform a specific task without returning a value. They can accept input parameters and modify the values of variables. Procedures are typically used to group related code together and make the program more organized and modular.
procedures are used when a specific task needs to be performed without a required result.
Procedure Example
def greetUser(name) :
print(f"Nice to meet you {name}")
greetUser("John"); // Output: Hello, John!
What is the main purpose of procedures?
Local Variables
Local variables are variables that are defined within a function and are only accessible within that function. They exist only during the function's execution, and once the function finishes, the local variables are destroyed and their values are lost.
def greet():
message = "Hello, World!" # 'message' is a local variable
print(message)
greet()
print(message) # This will raise an error because 'message' is not accessible outside the function
Global Variables
Global variables are variables that are defined outside of any function and can be accessed and modified by any function within the same program. They persist for the duration of the program's execution and can be used to share data across different parts of the program.
If we want to alter global variables from inside a function then we need to use the global keyword inside the function.
count = 0
def increment():
global count # Declare that we are using the global variable 'count'
count += 1 # Modify the global variable
def show_count():
print(f"Count: {count}")
increment()
show_count() # Output will be: Count: 1
Variable Scope
Scope refers to the region of a program where a variable is accessible. In Python, there are two main types of scope:
Local Scope
- A variable defined within a function has a local scope. It can only be accessed within that function.
- Once the function finishes executing, the local variables are destroyed.#
Global Scope
- A variable defined outside of all functions has a global scope. It can be accessed by any function within the program.
- Global variables persist throughout the program's execution
Optional Parameters
In Python, a function parameter can have a default value assigned to it, making it optional during the function call. This means that we can provide a default value for a parameter, which will be used if no value is explicitly passed when calling the function.
To define an optional parameter, we simply provide a default value to the parameter in the function definition. For example:
def greet(name, message="Hello"):
print(message + ", " + name)
In the above code, the parameter "message" is optional with a default value of "Hello". If no value is provided for "message" during the function call, it will default to "Hello". However, if a value is provided, it will override the default value.
We can now call the "greet" function with or without providing the "message" parameter:
greet("John") # Prints: Hello, John
greet("Jane", "Hi") # Prints: Hi, Jane
Optional parameters provide flexibility in function design and make the code more readable. They allow us to define default behaviors for functions while still allowing customization when needed. This can save us from writing multiple versions of the same function with slight differences in behavior.
Required Parameters
In Python, required parameters are the arguments that must be provided to a function when it is called. These parameters are mandatory and if not provided, the function will raise an error.
Required parameters are defined within the parentheses of a function's definition. When calling the function, the respective values for these parameters must be passed in the same order as they are defined.
def greet(name, age):
print(f"Hello {name}! You are {age} years old.")
greet("Alice", 16)
In the above example, the function greet requires two parameters: name and age. When calling the function, we pass the values for these parameters as "Alice" and 16 respectively, which will be used within the function's body.
If you forget to provide any of the required parameters when calling a function, Python will raise a TypeError with an appropriate error message indicating the missing arguments.
What is a keyword argument in Python?
Parameters vs Arguments
Parameters are variables declared in a function's signature to define the type and number of inputs the function can receive. They act as placeholders for data that will be passed to the function when it's called.
Arguments, on the other hand, are the specific values that are provided to the function when calling it. These values are assigned to the corresponding parameters within the function's body, allowing the function to work with the given data. In essence, parameters establish the structure of the input the function expects, while arguments provide the actual data that fits that structure.

What is the difference between parameters and arguments?
Introduction to Functions & Procedures
- Introduction to Functions
- Introduction to Procedures
- Local Variables
- Global Variables
- Variable Scope
Optional & Required Parameters
- Optional Parameters
- Required Parameters
- Parameters vs Arguments