Skip to content
Learnearn.uk » Python Unit Home » Functions and Procedures

Functions and Procedures

Functions

Introduction to Functions

What are Functions?

Functions are blocks of code that perform a specific task and return a value. 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)

Procedures

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!

Optional Params

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 Params

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.

Params vs Args

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.

Resources