0% found this document useful (0 votes)
33 views5 pages

Week 2 8 2nd Term Void and Return Function

1. The document discusses void and return functions at basic, intermediate, and advanced levels of complexity with examples. 2. At the basic level, functions demonstrate simple print and return behaviors. At the intermediate level, functions perform more complex calculations. 3. The advanced functions represent complex real-world scenarios, potentially interacting with databases or performing algorithm optimizations.

Uploaded by

Mahboob Riaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Week 2 8 2nd Term Void and Return Function

1. The document discusses void and return functions at basic, intermediate, and advanced levels of complexity with examples. 2. At the basic level, functions demonstrate simple print and return behaviors. At the intermediate level, functions perform more complex calculations. 3. The advanced functions represent complex real-world scenarios, potentially interacting with databases or performing algorithm optimizations.

Uploaded by

Mahboob Riaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Let's create examples of void (functions with no return value) and return functions in Python at

three different levels of complexity: basic, intermediate, and advanced.

Basic Level:

pythonCopy code
# Void Function (Basic)
def greet_basic(name):
print(f"Hello, {name}!")

# Return Function (Basic)


def add_basic(a, b):
return a + b

# Example Usage
greet_basic("John")
result = add_basic(3, 5)
print("Sum:", result)

Intermediate Level:

pythonCopy code
# Void Function (Intermediate)
def print_square_intermediate(num):
square = num ** 2
print(f"The square of {num} is: {square}")

# Return Function (Intermediate)


def calculate_average_intermediate(numbers):
total = sum(numbers)
average = total / len(numbers)
return average

# Example Usage
print_square_intermediate(7)
numbers_list = [2, 4, 6, 8, 10]
avg = calculate_average_intermediate(numbers_list)

1|Page
print("Average:", avg)

Advanced Level:

pythonCopy code
# Void Function (Advanced)
def generate_report_advanced(data):
# Complex logic to generate a report from data
# This function may interact with databases, APIs, or perform extensive computations
print("Report Generated Successfully")

# Return Function (Advanced)


def optimize_algorithm_advanced(data):
# Complex algorithm optimization
# This function may involve advanced algorithms, parallel processing, or machine learning
techniques
result = perform_optimization(data)
return result

# Example Usage
data_for_report = {"user": "Alice", "transactions": [120.50, 45.30, 67.80]}
generate_report_advanced(data_for_report)

data_for_optimization = [34, 12, 56, 78, 23, 90]


optimized_result = optimize_algorithm_advanced(data_for_optimization)
print("Optimized Result:", optimized_result)

In the advanced example, the generate_report_advanced function and


optimize_algorithm_advanced function represent more complex scenarios where the functions
perform intricate tasks or optimizations. The specific implementation would depend on the
requirements of your application.

Basic Level:

pythonCopy code
# Void Function (Basic)
def greet_basic(name):
print(f"Hello, {name}!")

# Return Function (Basic)


def add_basic(a, b):
return a + b

# Example Usage

2|Page
greet_basic("John")
result = add_basic(3, 5)
print("Sum:", result)

1. Void Function (Basic):


o greet_basic is a void function that takes a name parameter and prints a greeting
message to the console.
o It demonstrates a basic function that performs an action but doesn't return any
value.

2. Return Function (Basic):


o add_basic is a function that takes two parameters (a and b) and returns their sum.
o It showcases a basic function that performs a computation and returns a value.

3. Example Usage:
o Calls greet_basic with the argument "John" to print a greeting.
o Calls add_basic with arguments 3 and 5, stores the result in the variable result,
and prints it.

Intermediate Level:

pythonCopy code
# Void Function (Intermediate)
def print_square_intermediate(num):
square = num ** 2
print(f"The square of {num} is: {square}")

# Return Function (Intermediate)


def calculate_average_intermediate(numbers):
total = sum(numbers)
average = total / len(numbers)
return average

# Example Usage
print_square_intermediate(7)
numbers_list = [2, 4, 6, 8, 10]
avg = calculate_average_intermediate(numbers_list)
print("Average:", avg)

1. Void Function (Intermediate):


o print_square_intermediate is a void function that takes a number (num) as a
parameter, calculates its square, and prints the result.
o It introduces a more complex calculation compared to the basic example.

2. Return Function (Intermediate):

3|Page
o calculate_average_intermediate is a function that takes a list of numbers and
returns their average.
o It demonstrates a function that involves more complex computation but still
follows a straightforward logic.

3. Example Usage:
o Calls print_square_intermediate with the argument 7 to print the square of 7.
o Calls calculate_average_intermediate with a list of numbers and prints the
average.

Advanced Level:

pythonCopy code
# Void Function (Advanced)
def generate_report_advanced(data):
# Complex logic to generate a report from data
# This function may interact with databases, APIs, or perform extensive computations
print("Report Generated Successfully")

# Return Function (Advanced)


def optimize_algorithm_advanced(data):
# Complex algorithm optimization
# This function may involve advanced algorithms, parallel processing, or machine learning
techniques
result = perform_optimization(data)
return result

# Example Usage
data_for_report = {"user": "Alice", "transactions": [120.50, 45.30, 67.80]}
generate_report_advanced(data_for_report)

data_for_optimization = [34, 12, 56, 78, 23, 90]


optimized_result = optimize_algorithm_advanced(data_for_optimization)
print("Optimized Result:", optimized_result)

1. Void Function (Advanced):


o generate_report_advanced is a void function that takes a data parameter and
performs complex logic to generate a report.
o It illustrates a scenario where a function might interact with databases, APIs, or
involve extensive computations.

2. Return Function (Advanced):


o optimize_algorithm_advanced is a function that takes data and returns an
optimized result.
o It highlights a situation where a function performs advanced algorithmic
optimizations, potentially using techniques like machine learning.

4|Page
3. Example Usage:
o Calls generate_report_advanced with a sample data structure for a report.
o Calls optimize_algorithm_advanced with a list of numbers for optimization and
prints the result.

In the advanced example, the complexity of the tasks the functions perform is increased,
representing more sophisticated scenarios you might encounter in real-world applications. Keep
in mind that the exact implementation details would depend on the specific requirements of your
application.

5|Page

You might also like