View unanswered posts | View active topics It is currently Thu Jun 04, 2020 7:34 pm

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Using a Function within an Assignment
Author Message
Rookie

Joined: Sat Jul 11, 2015 3:53 pm
Posts: 6
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 perhapsfloat 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
Moderator

Joined: Tue May 19, 2015 3:07 pm
Posts: 91
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
Site Admin

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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
Rookie

Joined: Sat Jul 11, 2015 3:53 pm
Posts: 6
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 4 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning

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