How to Return a Value in Python- Calculate Rectangle Area – Programming, Pseudocode Example, C# Programming Example
Python 3

How to Return a Value in Python- Calculate Rectangle Area

We will often need to return a value from a function. We can do this with the return statement in Python. We just need to write this in the function definition:

Tip: the function stops immediately when return is found and the value is returned.

Here we have an example:

Now we can call the function and assign the result to a variable because the result is returned by the function:

We can also use return with a conditional to return a value based on whether a condition is True or False.

In this example, the function returns the first even element found in the sequence:

If we call the function, we can see the expected results:

Tip: if a function doesn’t have a return statement or doesn’t find one during its execution, it returns None by default.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.