0

For purpose of illustration, in pseudocode:

function addint(x,y,answer: integer)
  var
    condition: integer;

  begin
    if ((MAXINT - x) < y) then
      begin
        answer := x;
        condition := 1;    
      end
     else
       begin
         answer := x + y;
         condition := 0;
       end;  

     return condition;
   end.

In the example, answer is either called by reference or is a pointer. It holds the actual result to be passed back to the caller, and the function itself holds the exit code. The exit code follows the C tradition: 0 for normal exit and other values for abnormal exit. It is required because the operation is not always successful, depending on the values of x and y.

Is there anyway to write the same style of functions in Python without major modifications?

fhcluk
  • 9
  • 3
  • 3
    Return a tuple of values or an object...? Typically though you *raise an exception* if something goes wrong, you don't return a "falsey value" like `1`. – deceze May 31 '16 at 05:09
  • Multiple return arguments are usually handled by returning a tuple. After that, caller can do whatever he wants with those return values. – Łukasz Rogalski May 31 '16 at 05:10

1 Answers1

0

You can write functions that return multiple values in Python. Briefly,

def test():
    return 1, 2

x, y = test() #=> x = 1, y = 2
axblount
  • 2,639
  • 23
  • 27
  • 2
    Technically it returns single value (a tuple). But language has convenient syntactic sugar (iterable packing / unpacking) so it almost feel like you are returning multiple values. – Łukasz Rogalski May 31 '16 at 05:13