more than 1 return value per sub program?

Nov 11, 2014 at 6:44pm
Is it possible to have more than 1 return value from a subprogram? Please explain why
cant find answer anywhere : s
Nov 11, 2014 at 7:12pm
- you can pass the parameters as pointers, when calling the function. that way you can edit them inside the function and don't need to return them.
- you can use a vector or array or anything that can save more than one value.
Nov 11, 2014 at 7:13pm
You can't find an answer because you're using the wrong terminology. There are no "sub programs" in C/C++. There are, however, functions.

A function can only have one return value. But, that return value can be a structure that contains multiple values. Generally, you can either define your own data structure for the return type, or use std::pair or std::tuple.
Nov 11, 2014 at 8:12pm
If by sub-program you mean a sub-process, there are several ways to return values other than single integers. The easiest is probably a pipe, but you can also use files, shared memory and sockets. The exact method depends on the OS.

LB wrote:
You can't find an answer because you're using the wrong terminology. There are no "sub programs" in C/C++. There are, however, functions.

Standard C++ doesn't have a concept of sub-programs (sub-processes) but that doesn't mean the concept doesn't exist. Don't assume OP means something other than what he said. Ask if you're not sure.
Nov 11, 2014 at 9:29pm
You're giving LB a hard time about common terminology in CS, for which function is the correct equivalent in C and C++. Odds are on his response being correct.

I have never seen "subprogram" used as an alias for "subprocess".

[edit]
@Diblaliciouz
Yes, but not directly. A function only returns a single value. (That value may be a struct or a pointer to something like an array, but it is still a single value.)

A function may take as argument pointers or references which you can use set values outside the function.

Hope this helps.
Last edited on Nov 11, 2014 at 9:34pm
Nov 11, 2014 at 10:03pm
I assumed OP did not mean invoking other processes because usually the return code indicates success or the kind of error that occurred, and is generally not used for anything else.
Last edited on Nov 11, 2014 at 10:03pm
Topic archived. No new replies allowed.