Prerequisite: Functions in C/C++ A function in C can be called either with arguments or without arguments. These functions may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values. Hence the function prototype of a function in C is as below: |
Unlike other programming languages, python functions are not restricted to return a single type of value. If you look at the function definition, it doesn’t have any information about what it can return.
Let’s look at an example where the function will return multiple types of values.
def get_demo_data[object_type]: if 'str' == object_type: return 'test' elif 'tuple' == object_type: return [1, 2, 3] elif 'list' == object_type: return [1, 2, 3] elif 'dict' == object_type: return {"1": 1, "2": 2, "3": 3} else: return None
Returning Multiple Values in a single return Statement
We can return multiple values from a single return statement. These values are separated by a comma and returned as a tuple to the caller program.
def return_multiple_values[]: return 1, 2, 3 print[return_multiple_values[]] print[type[return_multiple_values[]]]
Output:
[1, 2, 3]
Python return statement with finally block
When return statement is executed inside a try-except block, the finally block code is executed first before returning the value to the caller.
def hello[]: try: return 'hello try' finally: print['finally block'] def hello_new[]: try: raise TypeError except TypeError as te: return 'hello except' finally: print['finally block'] print[hello[]] print[hello_new[]]
Output:
finally block hello try finally block hello except
If the finally block has a return statement, then the earlier return statement gets ignored and the value from the finally block is returned.
On the actual behavior, there is no difference. They all return None
and that's it. However, there is a time and place for all of these. The following instructions are basically how the different methods should be used [or at least how I was taught they should be used], but they are not absolute rules so you can mix them up if you feel necessary to.
Using return None
This tells that the function is indeed meant to return a value for later use, and in this case it returns None
. This value None
can then be used elsewhere. return None
is never used if there are no other possible return values from the function.
In the following example, we return person
's mother
if the person
given is a human. If it's not a human, we return None
since the person
doesn't have a mother
[let's suppose it's not an animal or something].
def get_mother[person]:
if is_human[person]:
return person.mother
else:
return None
Using def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
4
def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
This is used for the same reason as
def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
5 in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even though you don't need it that often.We've got 15
def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
6 and we know one of them has a knife. We loop through each def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
7 one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function because we know there's only one knife and no reason the check rest of the def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
6. If we don't find the def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
7 with a knife, we raise an alert. This could be done in many different ways and using def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
4 is probably not even the best way, but it's just an example to show how to use def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
4 for exiting a function.def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
Note: You should never do
def set_mother[person, mother]:
if is_human[person]:
person.mother = mother
2, since the return value is not meant to be caught.Using no def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
4 at all
def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
This will also return None
, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as
def find_prisoner_with_knife[prisoners]:
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition[]
return # no need to check rest of the prisoners nor raise an alert
raise_alert[]
4 in def set_mother[person, mother]:
if is_human[person]:
person.mother = mother
6 functions in languages such as C++ or Java.In the following example, we set person's mother's name and then the function exits after completing successfully.