What is the return value for a function that has no return statement defined?
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: | Show
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 StatementWe 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 Multiple Values Python return statement with finally blockWhen 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 Using return NoneThis tells that the function is indeed meant to return a value for later use, and in this case it returns In the following example, we return
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() 4This is used for the same reason as 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 6 and we know one of them has a knife. We loop through each 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 6. If we don't find the 7 with a knife, we raise an alert. This could be done in many different ways and using 4 is probably not even the best way, but it's just an example to show how to use 4 for exiting a function.
Note: You should never do 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 allThis will also return 4 in 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. What value is returned when function does not return any?If a function does not return any value ( also called returns void) then the return type of the function will be void.
What is the return value of a function?A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs.
|