StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Void functions and Fruitful Functions
Download
  1. Python
  2. Pyhton MCA (Machine Learning using Python)
  3. Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP
Scope of variables : File Handling
Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP

In Python, functions can be categorized based on whether they produce a value that can be used by the rest of the program. This distinction leads to two types of functions: fruitful functions (which return a value) and void functions (which do not).

1. Fruitful Functions

A fruitful function is a function that performs a task and returns a value back to the part of the code that called it. This is accomplished using the return keyword. The returned value can then be stored in a variable, printed, or used in any other expression.

  • Use Case: You use a fruitful function whenever you need a function to compute, calculate, or retrieve a piece of data and provide it back as a result. Think of it as asking a question and getting an answer.

Python

# --- Fruitful Function Example ---

 

# This function calculates the area of a circle and returns the result.

def calculate_circle_area(radius):

    """Calculates and returns the area of a circle."""

    pi = 3.14159

    area = pi * (radius ** 2)

    return area # The 'return' keyword sends this value back.

 

# We call the function and store its "fruit" (the returned value) in a variable.

circle_area = calculate_circle_area(5)

 

# Now we can use the returned value.

print(f"The area of the circle is: {circle_area}")

print(f"The area of a circle with double the radius is: {calculate_circle_area(10)}")


2. Void Functions

A void function is a function that performs an action but does not explicitly return a value. These functions are executed for their "side effects," such as printing output to the screen, modifying a mutable data structure (like a list), or writing to a file.

Technically, all functions in Python return something. If a function doesn't have a return statement, it automatically returns the special value None.

  • Use Case: You use a void function when the goal is to perform an action rather than to produce a value. Think of it as giving a command.

Python

# --- Void Function Example ---

 

# This function displays a formatted user profile but does not return anything.

def display_user_profile(username, age):

    """Prints a user's profile information."""

    print("--- User Profile ---")

    print(f"Username: {username}")

    print(f"Age: {age}")

    # There is no 'return' statement.

 

# We call the function to perform the action (printing).

display_user_profile("Rohit", 30)

 

# If we try to store the result, we will get 'None'.

result = display_user_profile("Neha", 29)

print(f"\nThe return value of the void function is: {result}")


Key Differences and How to Choose

Feature

Fruitful Function

Void Function

Primary Purpose

To compute and return a value.

To perform an action (a "side effect").

return Keyword

Must have a return statement with a value.

Does not have an explicit return statement.

Return Value

The value specified by return.

None.

How it's Used

The result is often assigned to a variable or used in an expression.

The function is called on its own line to execute its action.

Analogy

Asking a calculator for 5 + 3 and getting 8 back.

Telling a printer to "print a document."

When to choose:

  • If your function's name is a noun or describes a value (e.g., get_average, calculate_tax), it should probably be fruitful.

  • If your function's name is a verb describing an action (e.g., print_report, save_user_data), it should probably be void.

 

Scope of variables File Handling
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website