+ 3

Return

Can someone explain me "return" in python

7th Aug 2024, 4:22 PM
Jakob
Jakob - avatar
10 ответов
+ 6
Jakob , here is a short code sample that demonstrates the use of `return`: https://sololearn.com/compiler-playground/c8w79FUr1j7u/?ref=app
7th Aug 2024, 7:13 PM
Lothar
Lothar - avatar
+ 6
A function with return sends a value back to its caller. It is useful when you need the data for other expressions and computations in the program Whereas print prints the data on the screen when the function is called Also in relation to Function Termination: When a function executes a return statement, it ends immediately. If a function only has print statements, it continues executing until it reaches the end of the function or encounters a return statement.
7th Aug 2024, 4:54 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
+ 6
Here is an example to make this more clear You can see in the second part of the code how it is used to terminate the program while in the first to get annn expression # calculate the average grade def calculate_average(grades): if not grades: # list is empty? return 0 return sum(grades) / len(grades) # Function to determine the letter grade based on the average def determine_letter_grade(average): if average >= 90: return 'A' elif average >= 80: return 'B' elif average >= 70: return 'C' elif average >= 60: return 'D' else: return 'F' # List of grades grades = [8, 90, 12, 77, 88] # calling average = calculate_average(grades) # calling letter_grade = determine_letter_grade(average) # Output print(f"The average grade is: {average:.2f}") print(f"The letter grade is: {letter_grade}")
7th Aug 2024, 4:57 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
+ 3
Jakob yes For example def get_user_data(): return 'dan', 18, '17338' res = get_user_data() name, age, id = res print('the data gotten from user:', name, age, id)
8th Aug 2024, 6:44 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
+ 3
Jakob "return" statement in Python is used to exit a function and send back a value (or multiple values as a tuple) to the caller. The essential are: 1.Exiting the Function 2. Returning a Value
9th Aug 2024, 4:48 AM
Harimamy Ravalohery
Harimamy Ravalohery - avatar
+ 2
Thanks
8th Aug 2024, 10:12 AM
Jakob
Jakob - avatar
+ 2
Jakob your welcome , keep up the good work good luck
8th Aug 2024, 10:16 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
+ 1
Can you save multiple values ​​with return?
8th Aug 2024, 6:40 AM
Jakob
Jakob - avatar
+ 1
Please which app are u using to write ur codes on mobile phone
16th Aug 2024, 2:10 PM
Panacea. G
0
On my mobile phone I only use the code playground
16th Aug 2024, 2:23 PM
Jakob
Jakob - avatar