Object function not returning values

Hello,
My function is not returning any values.

main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(void) {
  
 txrx data;
 modules mods;
 
 int sa = 0;
 int da = 0;
 int status = 0;
 int function = 0;
 
 sa = 50; // sensor address
 da = 100; // actuator address
 
 mods.sensor(status,function);
 data.send_packet(sa, da, status, function);
 
 data.rec_packet();
 mods.actuator();
 
return 0;
}


mods.sensor(status,function);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int modules::sensor(int status, int function) {

 int sensor;

 cout << "Sensor: 0=NULL, 1=motion, 2=vehicle" << "\n";
 cin >> sensor;
 
 if (sensor == 0){
  status = 9;
  function = 9;
 }
 else if (sensor == 1) {
  status = 1;
  function = 200;
 }
 else {
  status = 1;
  function = 128;
 }
 
 return(status,function);
}


I expected 'mods.sensor(status,function);' to return the status and function variables. I then expected these to be picked up by 'data.send_packet(sa, da, status, function);' however they are not.

I know the answer is probably really simple however after searching the internet and looking through my book I cannot find out why 'data.send_packet(sa, da, status, function);' is not picking up on the status and function variables returned by 'mods.sensor(status,function);'.

Thank you in advance.
Functions can only return a single value. Its type could be some kind of composite, though. For example, you could return a pair<int, int>.

Another possibility is to use references/pointers to non-const arguments and return a value that way.

This is an interesting try, though:
return(status,function);

Unless I'm mistaken1, it is perfectly legal syntax and would return the last sub-statement evaluated (function).


1 I vaguely recall reading something similar to this in C++ Gotchas. I could be thinking of something else (and it wouldn't be the first time!)...
Last edited on
Oh! Sorry, I forgot to mention that, in main, you are not capturing the return value from the function.
1
2
3
4
5
int f() { return 1; }
//...
int a = 0;
a = f();
cout << a;




1

Last edited on
You sir are a true gentleman! Thank you very much for helping me understand my problem.

You would think that a function could return two values in the way I was trying to. I can not see any problems it could cause however I'm still a beginner in C++ and I'm sure the original developers thought about the consequences of doing so.

Thanks again!
Topic archived. No new replies allowed.