Close Navigation
Learn more about IBKR accounts
Python Function Tutorial – Part V

Python Function Tutorial – Part V

Posted April 3, 2020
Jay Parmar
QuantInsti

See Part IPart II , Part III and Part IV of this installment to learn more about Python functions.

Python functions with multiple arguments and a return statement

Both versions of the greet Python functions defined above were actually straightforward in terms of functionality that they perform. One more functionality that functions are capable of performing is to return a value to the calling statement using the keyword return. Consider a function that takes several parameters, performs some mathematical calculation on it and returns the output. For example:

# Function with two parameters ‘a’ and ‘b’
def add(a, b):
“””Computes the addition and returns the result.
It does not implement the print statement.”””
result = a + b # Computes addition
return result # Returns the result variable

This user defined function add takes two parameters a and b, sums them together and assigns its output to a variable result and ultimately returns the variable to calling statement as shown below:

# Calling the add function
x = 5
y =
print(f’The addition of {x} and {y} is {add(x, y)}.’)

We call the function add with two arguments x and y (as the function definition has two parameters) initialized with 5 and 6 respectively, and the addition returned by the function gets printed via the print statement as shown below:

The addition of 5 and 6 is 11.

Similarly, Python functions can also return multiple values based on the implementation. The following function demonstrates the same.

# Function definition
def upper_lower(x):
“””Returns the upper and lower version of the string.
The value must be a string, else it will result in an error.
This function does not implement any error handling mechanism.”””
upper = x.upper() # Convert x to upper string
lower = x.lower() # Convert x to lower string
return upper, lower # Return both variables upper and lower

The above upper_lower function takes one argument x (a string) and converts it to their upper and lower versions. Let us call it and see the output.

Note: The function upper_lower implicitly assumes to have a string as a parameter. Providing an integer or float value as an argument while calling will result in an error.

# Calling the function
upper, lower = upper_lower(‘Python’)
# Printing output
print(upper)
PYTHON
print(lower)
python

Here, the call to upper_lower function has been assigned to two variables upper and lower as the function returns two values which will be unpacked to each variable respectively and the same can be verified in the output shown above.

In the next installment, the author will discuss Python functions with default arguments.

Visit https://www.quantinsti.com/ for ready-to-use Python functions as applied in trading and data analysis.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.