Mẹo If a function doesn’t have a return statement, which of the following does the function return?
Kinh Nghiệm Hướng dẫn If a function doesn’t have a return statement, which of the following does the function return? Chi Tiết
Lã Tuấn Dũng đang tìm kiếm từ khóa If a function doesn’t have a return statement, which of the following does the function return? được Update vào lúc : 2022-09-01 14:02:01 . Với phương châm chia sẻ Bí quyết về trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi Read nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.Username* Please type your username.
Nội dung chính- Every Function returns SomethingWhat happens when the return statement has nothing?Python Functions can have multiple return statementsPython Function can return multiple types of valuesReturning Multiple Values in a single return StatementPython return statement with finally blockReferences:What does the function return if it doesn't have a return statement?When a function does not have a return statement What does the function return when called in Python?Which function does not use a return statement?What happens if a function doesn't return anything?
E-Mail* Please type your E-Mail.
Question Title* Please choose an appropriate title for the question so it can be answered easily.
Category* Please choose the appropriate section so the question can be searched easily.
Tags Please choose suitable Keywords Ex: question, poll.
Is this question is a poll? If you want to be doing a poll click here.
Details*
Type the description thoroughly and in details.
Ask Anonymously
Add a Video to describe the problem better.
Video type Choose from here the video type.
Video ID Put Video ID here: https://www.youtube.com/watch?v=sdUUx5FdySs Ex: "sdUUx5FdySs".
Get notified by email when someone answers this question.
By asking your question, you agree to the Terms of Service and Privacy Policy .*
- The python return statement is used in a function to return something to the caller program.We can use the return statement inside a function only.In Python, every function
returns something. If there are no return statements, then it returns None.If the return statement contains an expression, it’s evaluated first and then the value is returned.The return statement terminates the function execution.A function can have multiple return statements. When any of them is executed, the function terminates.A function can return multiple types of values.Python function can return multiple values in a single
return statement.
Let’s look a simple example to add two numbers and return the total to the caller.
def add(x, y): total = x + y return total
We can optimize the function by having the expression in the return statement.
def add(x, y): return x + y
Every Function returns Something
Let’s see what is returned when a function doesn’t have a return statement.
>>> def foo(): ... pass ... >>> >>> print(foo()) None >>>
Python Return NoneWhat happens when the return statement has nothing?
When the return statement has no value, the function returns None.
>>> def return_none(): ... return ... >>> print(return_none()) None >>>
Python Functions can have multiple return statements
def type_of_int(i): if i % 2 == 0: return 'even' else: return 'odd'
Python Function can return multiple types of values
Unlike other programming languages, python functions are not restricted to return a single type of value. If you look the function definition, it doesn’t have any information about what it can return.
Let’s look 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.
def hello(): try: return 'hello try' finally: print('finally block') return 'hello from finally' print(hello())
Output:
finally block hello from finally
References:
- Python Official Docs
Post a Comment