Python Tutorial 22 : How to return a value from a function

As mentioned previously, the “Def” statement defines a function. So how to return a value from a function?


We can use the return statement to return a value from a function.

For example:

[vtftable cols=”{0}0:fff2cc;{/}”]
def dummyfunction (i):{;n}    return i;nn;

In other languages, you may have to define the datatype of the output. However, in Python, the datatype is automatically determined.

This also means in python, the datatype of a result from a python function can be very variant it can be a number in scenario “A” while a string in scenario “B”


[vtftable cols=”{0}0:fff2cc;{/}”]
print(dummyfunction(“10 + 1”)){;n}
10 + 1;nn;

where “10  + 1 ” is a string as the input is a string.

[vtftable cols=”{0}0:fff2cc;{/}”]
print(dummyfunction(10 + 1)){;n}

where 11 is a number (result of 10 + 1).

Leave a Comment

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