View unanswered posts | View active topics It is currently Mon Sep 22, 2014 6:21 pm






Reply to topic  [ 2 posts ] 
Returning a struct from a function 
Author Message
Rookie

Joined: Mon Sep 19, 2011 5:25 pm
Posts: 3
Post Returning a struct from a function
Hello,

I have a typedef struct that I am trying to return from a function; I get the error
Code:
**Error**:Invalid procedure return type


My SSCCE is as follows:

Code:
typedef struct {
  short x_pos;
  short y_pos;
} JoystickPos;

JoystickPos getPosition(TJoystick j, bool isFirstJoystick, JoystickSide side) {
  getJoystickSettings(j);
  JoystickPos p;
  switch (side) {
  case LEFT_SIDE:
    p.x_pos = (isFirstJoystick ? j.joy1_x1 : j.joy2_x1);
    p.y_pos = (isFirstJoystick ? j.joy1_y1 : j.joy2_y1);
    break;
  case RIGHT_SIDE:
    p.x_pos = (isFirstJoystick ? j.joy1_x2 : j.joy2_x2);
    p.y_pos = (isFirstJoystick ? j.joy1_y2 : j.joy2_y2);
    break;
  }
  return p;
}


Also, what is
Code:
**Info***:'struct' variable 'j' automatically converted to call by reference
in the getPosition declaration?

Thanks!


Mon Sep 19, 2011 5:31 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Returning a struct from a function
Hi there,

You can't return a struct from a function, unfortunately. As for the pass by reference thing, think of it as a pointer.
Code:
// The & operator allows you to pass the argument by reference
// that means you are operating on the actual variable and not a copy
// of it.  Any changes you make, are reflected in the variable you
// passed to it.
void somefunc(int &foo)
{
  // increment foo
  foo=foo+1;
}

task main ()
{
  int baz = 0;
  nxtDisplayTextLine(1, "before: %d", baz);
  somefunc(baz);
  nxtDisplayTextLine(2, "after: %d", baz);
  while(true);
}

What I tend to do is use the & operator to pass structs to my functions that I want modified.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Sep 20, 2011 6:10 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.