Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Multiple return values

Name: Anonymous 2018-09-12 18:11

I've always found it weird that a function can take multiple arguments, but it can only return a single value. I guess you can make an Object array, and use that as a hacky workaround to return multiple values (of different data types instead of them all being the same, i.e. int[]).
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
What are your thoughts on having return values of different data types?

Functional programming apparently does away with return statements entirely, what's up with that?

Name: Anonymous 2018-09-13 1:37

I've always found it weird that a function can take multiple arguments, but it can only return a single value
Functions can only take a single argument and return a single value.

Functional programming apparently does away with return statements entirely, what's up with that?
Instead of writing return x you just write x.

Name: Anonymous 2018-09-13 1:41

>>2
Functions can only take a single argument
not in OOP
this is kind of pseudocode but you get the idea
public Object[] whatever(int x, string y, boolean z) {
//Object is a superclass for all other classes
Object objArray[] = new Object();
objArray[0] = x;
objArray[1] = y;
objArray[2] = z;
return objArray;
}

but it'd be cool if you could return separate things instead of the superclass array workaround

Name: Anonymous 2018-09-13 2:39

void return_multiple_values(int input,int* outp1,int* outp2){
*outp1=input;
*outp2=input;

}

Name: Anonymous 2018-09-13 3:15

can you pass args by reference in java?

Name: Anonymous 2018-09-13 4:36

Name: Anonymous 2018-09-13 6:07

I know, IHBT

you can return multiple values in some languages, e.g. Lisp. it usually requires a special keyword or sytntax though. some languages "fake" multiple returns with ad-hoc collections. funnily enough, Python doesn't have native multiple returns and fakes them with tuples but it has such a nice syntax for it that it looks very natural, surprisingly better than CL's (multiple-value-bind).

In [1]: def multret(a):
return 1,2,3
...:

In [2]: x, y, z = multret(0)

In [3]: x
Out[3]: 1

In [4]: y
Out[4]: 2

In [5]: z
Out[5]: 3

Name: Anonymous 2018-09-13 8:55

type Prague = Either<(Hax, My, Anus), FatLazyBydlo>;

Name: Anonymous 2018-09-14 2:33

>>3
Not sure what OOP has to do with this.
A function with multiple arguments, say \(f = \lambda x, y. x + y\) (or \(f(x, y) = x + y\)) is basically the same as \(f = \lambda x. \lambda y. x + y\)
Considering that every function takes a single argument, and that the so called multi-argument functions are simply nested functions, one can see why you can't directly return multiple values.

but it'd be cool if you could return separate things instead of the superclass array workaround
I am far from a Java expert but I am pretty sure that you can just make something like
class Tuple<A, B> { public A fst; public B snd; }
Which is type safe (as far as type-safety in Java goes).

Name: Anonymous 2018-09-14 2:55

>>9
Considering that every function takes a single argument
Then explain OOP?
so called multi-argument functions are simply nested functions
What? Are you thinking in terms of functional programming? I am coming from a mostly Java background.

How is this not a function with multiple arguments?
public string greeter(int age, string name) {
string message = "";
message += "Hello, ";
message += name;
message += "! You are "
message += age;
message += "years old!";
return message;
}

I know that's sloppy but I'm tired, but it clearly takes two arguments

Name: Anonymous 2018-09-14 6:33

>>10
>>9 is a functional anus who doesn't understand other paradigms and your're are a Java anus who only understands Java. but I'll expalin so please check my dubs in return:

in lambda calculus and in some functional languages like haskal, functions take on argument. if you don't know lambda calculus, you can imagine this as a typical function in math. consider this simple linear function: {code f(x) = 2x +1}.

of course, it is possible to create things that act like multiple-argument functions in this paradigm. this is because functions can return other functions: so an addition function will take only the first operand as its argument (e.g. 2), and it will output another function that adds it to a second operand (so in our example, it will be a function that adds 2 to its argument). you then apply taht function to the second operand to get the results. you can 'fake' this in a language like python with partial applications {(code functools.partial})

if this sounds tedious, that's because it is. fortunately, language designers knew it and they came up with the idea of currying, which means that either the syntax or the compiler does this stuff for you and you don't need to think about it most of the time. but it comes in handy if you need it.

Name: Anonymous 2018-09-17 7:17

Actually, if we programmed in a CPS-way (and thus arguments = return values) we could easily have multiple return values, consider:

(define (divmod x y k)
(k (/ x y) (% x y)))

(divmod 10 0 (lambda (dived moded) ...))

Name: Anonymous 2018-09-17 7:19

>>12
but CPS-way is a bigger anal deformation of brain than stack-based languages. it might make sense for some compilers to organize code this way but not for humans to write it

Name: Anonymous 2018-09-17 7:26

>>13
Consider
(define (divmod x y)
(/ x y)
(% x y))

(define-multi (dived moded) (divmod 10 0))
...

Compiling to the above

Name: Anonymous 2018-09-17 7:35

just use callbacks lmao

Don't change these.
Name: Email:
Entire Thread Thread List