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;
[/vtftable]

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;
[/vtftable]

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

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

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

Leave a Comment