View unanswered posts | View active topics It is currently Sun Nov 17, 2019 8:07 am






Reply to topic  [ 4 posts ] 
Using a Function within an Assignment 
Author Message
Rookie

Joined: Sat Jul 11, 2015 3:53 pm
Posts: 6
Post Using a Function within an Assignment
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 have read on these forums Ford Perfect's code for rounding a float into an integer (very helpful, thank you so much!). I am wondering if ROBOTC will allow me to use this function (or any function for that matter) in an assignment if that assignment falls within a declaration.

Code:
int round(float f)
{
  if(f>0) return (int)(f + 0.5);
  else    return (int)(f - 0.5);
}

float x = 1.4;
int y = round(y);


Also, can I use mathematical expressions in an assignment if that assignment falls within a declaration?

Code:
float x = 1.4;
int y = round(x) + 3;


And lastly, a bit off topic, but can I call a function within a declaration/assignment (or really anytime at all) by inputting a mathematical expression instead of a variable?

Code:
float x = 1.4;
int y = round(x + 3);

//or perhaps

float x = 1.4;
int y = 0;
y = round(x + 3);


Will that pipe through or do I have to do the calculation before calling the function (using some sort of a temporary variable) because functions can only handle variables as inputs?

Thank you for the help!


Sat Jul 11, 2015 4:27 pm
Profile
Moderator
Moderator

Joined: Tue May 19, 2015 3:07 pm
Posts: 91
Post Re: Using a Function within an Assignment
Every single one of the above examples works in ROBOTC. Just remember when it comes to a function that you have to have declared the function before you can use it. Mathematical order of operations exists inside of the language.


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

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Using a Function within an Assignment
You no longer need to use your own function to do a round(). That is now built-in to ROBOTC:
Code:
float foo = 0.6;
int baz = round(foo);

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


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

Joined: Sat Jul 11, 2015 3:53 pm
Posts: 6
Post Re: Using a Function within an Assignment
Thanks for these responses! It sounds like I can quite fluid in my assignments due to ROBOTC's flexibility.


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