Additional programming techniques

Using Subroutines in Programming

Subroutines, including functions and procedures, are blocks of code designed to perform specific tasks. They help in producing structured and modular code, making it easier to read, maintain, and debug.

Definition: Function and Procedure

  • A function returns a value

  • A procedure does not return a value

  • Both a procedure and function are named blocks of code that can be called within a program

  • Both a proecedure and function can have parameters that allow values to be passed in as arguments and used within

Procedures

Procedures are subroutines that perform a specific task but do not return a value.

Declaration and Usage

procedure procedure_name(parameters)
    // Code to execute
endprocedure
procedure agePass()
    print("You are old enough to ride")
endprocedure

procedure printName(name)
    print(name)
endprocedure

procedure multiply(num1, num2)
    print(num1 * num2)
endprocedure

Calling a Procedure

agePass()
printName("Alice")
multiply(3, 4)

Functions

Functions are subroutines that perform a specific task and return a value.

Declaration and Usage

function function_name(parameters)
    // Code to execute
    return value
endfunction

Examples

function squared(number)
    squared = number * number
    return squared
endfunction

Calling a Function

print(squared(4))  // Output: 16
newValue = squared(4)

Key Points

  • Modularity: Subroutines help break down complex problems into smaller, manageable parts.
  • Reuse: Code within subroutines can be reused across different parts of the program.
  • Maintainability: Easier to update and maintain code within subroutines.
  • Testing: Individual subroutines can be tested independently.

By understanding and using subroutines effectively, you can write cleaner, more organized, and maintainable code.

Functions and Procedures in Python

Procedures in Python

Procedures are defined using the def keyword and do not return a value.

Example

def age_pass():
    print("You are old enough to ride")

def print_name(name):
    print(name)

def multiply(num1, num2):
    print(num1 * num2)

Calling Procedures in Python

age_pass()
print_name("Alice")
multiply(3, 4)

Functions in Python

Functions are also defined using the def keyword but return a value using the return statement.

Example

def squared(number):
    return number * number

Calling a Function in Python

print(squared(4))  # Output: 16
new_value = squared(4) # new_value becomes 16

Parameters and Arguments

Parameters are variables listed inside the parentheses in the function definition. They act as placeholders for the values that will be passed to the function when it is called.

Arguments are the actual values passed to the function when it is called. These values replace the parameters in the function definition.

Example

# Function definition with parameters
def greet(name, age):
    print(f"Hello, {name}! You are {age} years old.")

# Function call with arguments
greet("Alice", 30)

Explanation

  • Function Definition: def greet(name, age):
  • name and age are parameters.
  • Function Call: greet("Alice", 30)
  • "Alice" and 30 are arguments.

Key Points

  • Parameters: Variables defined in the function signature.
  • Arguments: Values provided to the function when called that will replace the paramaters in the subroutine.
  • Positional Arguments: Arguments are passed in the same order as the parameters.
Previous
Use of arrays