Page 1 of 1

My Block equivalent in NXC

Posted: 28 Apr 2011, 01:59
by bungeshea
I am just starting to learn NXC, and I was wondering if there was an equivalent for NXT-G My Blocks in NXC :?:

Re: My Block equivalent in NXC

Posted: 28 Apr 2011, 02:17
by muntoo
'My Blocks' in NXC probably have more functionality than NXT-G My Blocks. :)

They're called subroutines/procedures and functions.

Subroutines:

Code: Select all

void MyVoid()
{
    TextOut(0, 0, "Hello MyVoid!");
    Wait(2000);
}

task main()
{
    MyVoid();
}
Guess what that code does?

-----

I recommend learning C++ (or at least, the C parts of it) from this tutorial. It's explained very well for a beginner.

Re: My Block equivalent in NXC

Posted: 28 Apr 2011, 03:52
by bungeshea
muntoo wrote:'My Blocks' in NXC probably have more functionality than NXT-G My Blocks. :)

They're called subroutines/procedures and functions.
Yes, but is there any way to save a section of code that you can call on in any of your programs?

Re: My Block equivalent in NXC

Posted: 28 Apr 2011, 04:04
by mattallen37
Yes there is. Make a library.

What I do, is have a folder that contains all my libraries, and include that path with the compiler settings. If I want to use one of libraries, all I have to do is #include it, and all the functions are available.

Re: My Block equivalent in NXC

Posted: 28 Apr 2011, 04:07
by muntoo
Ah, I get what you're talking about now. Use #includes.

A demonstration:

main.nxc

Code: Select all

#include "myblocks.nxc"

task main()
{
    HelloWorld();
}
myblocks.nxc

Code: Select all

void HelloWorld()
{
    TextOut(0, 0, "Hello, World!", 0);
    Wait(2000);
}
Just put #include "myblocks.nxc" in all of your programs from now on. (And don't forget to put "myblocks.nxc" in the same folder as each of your programs.)

-----

A better way would be to create a library, as Matt (who ninja'd me) suggested. That way, your "myblocks.nxc" code won't have to be compiled unnecessarily for one function/"block" you want to use out of a few unrelated hundred.

Re: My Block equivalent in NXC

Posted: 28 Apr 2011, 04:27
by mattallen37
muntoo wrote:...Just put #include "myblocks.nxc" in all of your programs from now on. (And don't forget to put "myblocks.nxc" in the same folder as each of your programs.)

-----

A better way would be to create a library, as Matt (who ninja'd me) suggested. That way, your "myblocks.nxc" code won't have to be compiled unnecessarily for one function/"block" you want to use out of a few unrelated hundred.
Muntoo, I think you missed my point. You do not have to save the libraries (or "myblocks.nxc") in the same directory... you can make the compiler look in an additional directory (where all the libraries or whatever else is located). It keeps me from have a zillion duplicates all over the place. It also means I only have to modify it in one place, and everything get modified.

Re: My Block equivalent in NXC

Posted: 21 May 2011, 04:26
by bungeshea
mattallen37 wrote: ... you can make the compiler look in an additional directory (where all the libraries or whatever else is located).
How do you do that?

Also is there any way to pass parameters into and out of voids? (Like data wires in NXT-G?)

Re: My Block equivalent in NXC

Posted: 21 May 2011, 04:50
by mattallen37
Like this:
BCC Compiler settings.jpg
Yes you can pass values in and out of a void function. Here is an example:

Code: Select all

void SetLimits(int value, int min, int max, int &result){
  if (value>max)value=max;
  if (value<min)value=min;
  result=value;
}
See how I did that? In that simple (example) function, I passed three ints into it (int value, int min, int max), and it send one back (int &end_value). The "&" means that it is an "output wire".

However, for that specific function, I would probably normally use something more like this:

Code: Select all

int SetLimits(int value, int min, int max){
  if (value>max)value=max;
  if (value<min)value=min;
  return value;
}
Instead of calling the function like this "SetLimits(value, min, max, result);", you would call it like this "NumOut(0, LCD_LINE1, SetLimits(value, min, max));". Because it is a "int" function, it has a numerical value (chosen by the "return" command).

Also, you are not limited to passing ints in and out of functions. You can use bool, char, byte, int, Uint, long, Ulong, array, string...

Re: My Block equivalent in NXC

Posted: 21 May 2011, 05:09
by bungeshea
Thanks! :D