RETURN VALUE

RETURN VALUE

In Python, you can return values from functions using the return statement. The return statement allows you to send a value back to the caller of the function. Here's an example code that demonstrates returning values in Python: distance between two points python code
       
       def add_numbers(a, b):
    return a + b

result = add_numbers(5, 3)
print(result)  # Output: 8




In the above example, we have a function named add_numbers that takes two arguments a and b. Inside the function, we use the return statement to send back the sum of a and b.

When we call the add_numbers function with 5 and 3, it returns the sum 8. We store the returned value in the result variable and then print it to the console.

Here's another example that demonstrates returning multiple values from a function:


def get_name_length(name):
    length = len(name)
    is_long = length > 5
    return length, is_long

name_length, is_name_long = get_name_length("Alice")
print(name_length)  # Output: 5
print(is_name_long)  # Output: False
In this example, the function get_name_length takes a name as an argument. Inside the function, we calculate the length of the name using len(name) and store it in the length variable. We also check if the name is considered long (having more than 5 characters) and store the result in the is_long variable.
Finally, we use the return statement to return both the length and is_long values. When we call the get_name_length function with the name "Alice", it returns 5 as the length and False as the is_long value. We store these returned values in separate variables (name_length and is_name_long) and print them to the console.





Comments

Popular posts from this blog

ILLUSTRATIVE PROGRAMS IN PYTHON

Fibonacci Series

The Fibonacci series