View unanswered posts | View active topics It is currently Mon Jun 25, 2018 8:09 am

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

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

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

My SSCCE is as follows:

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

JoystickPos getPosition(TJoystick j, bool isFirstJoystick, JoystickSide side) {
  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);
  case RIGHT_SIDE:
    p.x_pos = (isFirstJoystick ? j.joy1_x2 : j.joy2_x2);
    p.y_pos = (isFirstJoystick ? j.joy1_y2 : j.joy2_y2);
  return p;

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


Mon Sep 19, 2011 5:31 pm
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
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.
// 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

task main ()
  int baz = 0;
  nxtDisplayTextLine(1, "before: %d", baz);
  nxtDisplayTextLine(2, "after: %d", baz);

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.