Python Forum
havent programmed in years - confused by why RETURN is not returning
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
havent programmed in years - confused by why RETURN is not returning
#4
The return statement returns values from a function.
def add(a, b):
    result = a + b
    return result

total = add(2, 3)
print(total)
Output:
5
This part:
def add(a, b):
creates a function named "add" and says it expects 2 arguments.
This part:
    result = a + b
    return result
is the body of the function. The body defines what the function does. Here the body adds the arguments a and b and returns the result.

This part:
total = add(2, 3)
calls the function and assigns the return value to a variable named "total".

All functions return a value, even if they don't contain a return statement. If a function doesn't have a return statement, the return value is None.

What you tried to do is called "pass by reference". In pass by reference, a function is passed arguments meant to hold the return value for the function. Python does not support pass by reference. In Python you pass arguments used by the body of the function, and use "return" to return values from the function. To use the function return values, the caller must assign the return value(s) to a variable.
Reply


Messages In This Thread
RE: havent programmed in years - confused by why RETURN is not returning - by deanhystad - Mar-25-2023, 02:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,202 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 1,032 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,686 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,806 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 5,097 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,286 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 3,118 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,565 Feb-10-2020, 09:15 AM
Last Post: snippsat
  'Age' categorical (years -months -days ) to numeric Smiling29 4 3,033 Oct-17-2019, 05:26 PM
Last Post: Smiling29
  Create a monthly mean column in multiple years fyec 1 4,095 Jun-21-2018, 03:57 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020