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?
20 replies to this topic
#1
Posted 13 August 2010 - 05:37 PM
|
|
|
#2
Posted 13 August 2010 - 05:53 PM
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)
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
Posted 13 August 2010 - 05:54 PM
#4
Posted 13 August 2010 - 05:58 PM
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
Posted 13 August 2010 - 06:02 PM
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
Posted 13 August 2010 - 06:06 PM
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
Posted 13 August 2010 - 06:09 PM
I know, its just not as efficient as an assembly implementation.
#8
Posted 13 August 2010 - 06:10 PM
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
Posted 13 August 2010 - 06:16 PM
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
Posted 14 August 2010 - 04:28 AM
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++
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.).
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.
There is bound to be something useful somewhere.
#11
Posted 14 August 2010 - 05:37 AM
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
Posted 14 August 2010 - 06:42 AM
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.
There is bound to be something useful somewhere.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top










