View unanswered posts | View active topics It is currently Wed Jun 20, 2018 8:25 pm






Reply to topic  [ 3 posts ] 
Global versus Local Variables within Functions 
Author Message
Rookie

Joined: Sat Jul 11, 2015 3:53 pm
Posts: 6
Post Global versus Local Variables within Functions
Hello there! First time poster. I am playing around with creating functions in ROBOTC and have a couple of questions I was hoping the community could help answer. I didn't want to overload this one post though since each question is a little different, so I'm going to break it up into a few posts

I'm a bit confused how to navigate the difference between global and local variables in ROBOTC. Am I able to execute a function which uses global variables when I call the function? For example:

Code:
bool forwards = true;

bool moveforwards() {
   motor[port1] = 63;
   motor[port2] = -63;

   forwards = true;
   return forwards;
   }

task main() {
   forwards = moveforwards();
   }


Or instead do I have to define a local variable within the function to pass along?

Code:
bool forwards = true;

bool moveforwards() {
   motor[port1] = 63;
   motor[port2] = -63;

   bool forwards_temp = true;
   return forwards_temp;
   }

task main() {
   forwards = moveforwards();
   }


What would happen if I did the 2nd example, but declared the variable name "forwards" within the moveforwards function instead of "forwards_temp"? Would I get a compile error because there are two different variables, one global and one local, being declared with the same name? Or would ROBOTC just take the move recent declaration/assignment as the final one?

Thanks for the help!


Sat Jul 11, 2015 4:11 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Global versus Local Variables within Functions
Hey there,

In your first example you return the value of a variable that is already global in its nature, which is not really necessary.

You *can* have multiple variables with differing scopes (global vs local), but that may lead to bad programming habits, which you should avoid. The standard best practice for variables is: make their scope as limited as possible. That means that if a variables does not need to be global, then make it local. Resort to globals only when you have to.

Also, this is perfectly OK to do:
Code:
bool forwards = true;

bool moveforwards() {
  motor[motorA] = 63;
  motor[motorB] = -63;
  return true;
}

task main() {
  forwards = moveforwards();
}

_________________
| 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]


Sun Jul 12, 2015 6:46 am
Profile WWW
Rookie

Joined: Sat Jul 11, 2015 3:53 pm
Posts: 6
Post Re: Global versus Local Variables within Functions
Thanks for the info/advice! This helps a lot.


Sun Jul 12, 2015 4:41 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.