Jump to content

Why not more than one return value?

- - - - -

  • Please log in to reply
20 replies to this topic

#1
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Why can't you have more than one return value from a function? All that's happening is the return values are being pushed onto the stack in the function and the main function pops them off, so why can't there be more than one? Would it just be too complicated to code that?

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
You could attempt an returning an array, linked list, or other list to return with multiple values in it.

Or if it's OOP you could define a few temp variables and just modify those and not return anything (or true/false)

Or if you pass the variables as references you could just modify the variables and return nothing (or true/false)

#3
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
return 2 values - C++ Forums

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
The link suggests returning a structure with variables... which is similar to a class except it has no functions... if I remember correctly my teacher said the only advantage of using a struct instead of a class is for saving purposes... as it's easy to save/read a structure into a file, correct?

#5
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I know what to do if I want to, I'm just wondering because it would be easily possible in assembly, just push two from the function and pop two in the main part of the program. I was just wondering why they don't support anything like that in C/C++.

#6
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Probably because it's not really needed. A function/method should only perform one task... but like me and jwixie said it is very possible, but seems like it could get messy if you have a hundred structs each custom for each function.

#7
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I know, its just not as efficient as an assembly implementation.

#8
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
Well, as a newbie I can;t really say much about the technical detail, but a strut is public by default.... but that's the major difference between a class and a struct. My professor said they are virtually the similar thing, but the default is different.

#9
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I'm not saying it's more efficient to use a class, I'm saying it's more efficient to use the stack, which is a section of memory given to each process in a LIFO (Last In, First Out) format. So if i put 2 things on the stack (push them onto the stack) then the first thing i will pull off the stack (pop) is the 2nd thing I put on.

#10
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
Look why it is not implemented is most probably because of the idea of a function. The functions are based on the mathematical form of a function. So for example this function:
F(v) = v*v - v
would be made into this in C++

int F(int v) {

  return v * v - v;

}


Simple as that, it's a legacy of mathematics I guess. There is languages that can return several values at the same time like Ruby. But well the original C is an old language and also C++. And the designer of C also had a degree in applied mathematics from his University. Actually most languages made at that time were trying to stay as close to math as possible because most designers were in some way mathematicians(Engineers, physics, etc. etc.).
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.

#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Only difference between class and struc in C++ is default data visibility. In C++, structures can have constructors, destructors and methods, just like classes.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#12
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts

Flying Dutchman said:

Only difference between class and struc in C++ is default data visibility. In C++, structures can have constructors, destructors and methods, just like classes.

Actually, if you do not add any of those things then the structure is handles like a normal C structure and can be created the old way.
MyStructure structure = {data, data, data, data} 
And you can also do the old-school version of inheritage for structures.
But as soon as you add a method it's turned into what you said, a proxy class with just a different default visibility. Though I don't get the idea why they added this to the structure functionality?
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users