Go Down

Topic: Syntax Question () (Read 4472 times) previous topic - next topic

myggle

I am new to Arduino and my first Uno is still in the mail, but I'm trying to watch every tutorial I can stomach, plus also reading a lot to at least hit the ground crawling.

I can see myself understanding most of this new language (code), but for the life of me, I cannot comprehend the use of () with no characters in between.  Can someone please enlighten me?  I've tried the reference page, Google search, forum search and I am likely just searching incorrect language.  For the sake of this question, I will say that I comprehend (or will comprehend) why the parenthesis are used with characters in between, but why am I seeing them in many sketches with no characters therein?

TYIA

larryd

Give us an example of what you are talking about
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

AWOL

Quote
I cannot comprehend the use of () with no characters in between.

Usually, it's calling a function that takes no parameters.
Code: [Select]
void foo (void)
{
 Serial.println ("Foo!");
}

//
//
//
  foo ();  // causes "Foo!" to be printed
}


If you're asking "Why not just "foo;" ? ", then the answer to that is, that there you're not calling the function "foo", you're just testing to see if the function exists (i.e. that the pointer to the function is not NULL, which it won't be, or else the compiler would've thrown a wobbly) and chucking away the result, which is pointless.

jstarcher

In C, the empty parens can mean different things.

As others have said, it sounds like you are referring to this syntax:

Code: [Select]
function_name()

C and C++ use the parentheses because it is not uncommon to actually pass a function itself as an argument.  While this kind of syntax may be unusual in Arduino coding, it is quite common in applications.

For instance, when calling a the qsort function, one of the parameters is a second function, whose job is it to actually do the compare.  In that case, one uses the function name without the parens:

Code: [Select]

qsort (my_array, 6, sizeof(int), comparetor_function );


http://www.codingunit.com/c-reference-stdlib-h-function-qsort

myggle

Thank you all for the replies.  I guess I'm just a sucker for simplicity, example as follows;

void setup ()
vs.
void setup

I get that it's something I'm going to have to come to terms with, but being so new to coding, it just appears to be one of those things that makes learning that much harder.  If the parenthesis were limited to something that needed to be expressed, it would make sense, but the lack of any keystrokes in between those parenthesis coupled with the fact that there is no mention of their importance in the reference page just makes it appear to be an invisible hurdle that I have to jump over despite it not even being there.  Curly braces have a specific function, semi colons have another specific function, parenthesis are there just because.  Am I understanding this correctly?

KenF

It defines the item as a FUNCTION.

Some functions have values passed to them, others don't.  If they don't then there is nothing to put inside the brackets.

The example void setup() This tells the compiler that this is where you are defining your setup function.
Without the brackets you'd just be defining a variable.  Since variables can only hold a single value, it wouldn't be very productive in this case.


myggle

Thank you Ken for the great answer, and please forgive me for kicking this horse, as I don't yet know how to tell that it is dead or not, but if there is no function to be defined(), what then is the logical explanation for the parenthesis being there in the first place?

I can only assume they are there to tell the compiler that no function is being defined at this time, which then makes me ask how this rule is not applied globally?  Like to state that there is nothing to state.

KenF

#7
Oct 17, 2014, 05:51 am Last Edit: Oct 17, 2014, 05:57 am by KenF Reason: 1

Thank you Ken for the great answer, and please forgive me for kicking this horse, as I don't yet know how to tell that it is dead or not, but if there is no function to be defined(), what then is the logical explanation for the parenthesis being there in the first place?

Just because a function requires no details to be passed to it, doesn't mean there's no function to be defined.

Consider a real world appliction.  If you tell someone to paint, they'll need to know WHAT to paint and what colour to paint it.  Even more importantly, If they've never done the job before, you'll also have to tell them how to do it.   Now consider if you want them to somersault.  This action doesn't require you to give them any details, but it's an action nonetheless.  So if they don't know what that means you'll still have to describe how to perform a somersault.

So it is with setup().  At the begining of your sketch, your microcontroler will run the function setup().  But somewhere, you are going to have to give it the details of what that entails.

To define your setup function you use the line setup() followed by some open and closed braces and all of the details of the setup function described within.

Typically you'd have something like this
Code: [Select]

void setup()
{
runningTotal=0;
Serial.begin(9600);
}

This whole section is describing the setup function.  Now, whenever you want to call it anywhere in your program, you can simply use setup(); (note the semicolon at the end of the line.

In reality, setup is a pretty poor example because it's unlikely you'll want to call your setup function again, but you could if you want to.  And when you do, those () are going to tell your microcontroller that you want it to run off and do something.  

Notice that within this small example, we are also calling a function Serial.begin(9600).  This is not just setting a local variable to 9600, there's a bit of technical stuff that needs to be done in the background. (UART registers have to be setup to make the default baud rate 9600 and such stuff).  Fortunately, you don't need to know the details of how it does this task, because someone else has gone to the trouble to define that function for you already.

myggle

Thank you for taking the time to hold my hand on this.  I believe the concept is permeating my brain now.  Some years back I privately studied law, and it was common practice to define important words at the beginning of a code or statute (void setup) {}.

I see now that the () defer the reader (compiler) to also give attention to the {} where the actual defining occurs.  If I the code writer choose to really complicate the code and define these functions at first use of them, I can just leave the void setup() {} blank and incur the obligation to later define those functions/parameters at their first instance (and possibly with each instance after) later on in the program.  Once again, this would be the coder's prerogative, regardless of how asinine.

In your opinion, do I about have that right?

AWOL

#9
Oct 17, 2014, 01:54 pm Last Edit: Oct 17, 2014, 02:01 pm by AWOL Reason: 1
Sort of right.
Unfortunately, Arduino muddies the water a little.
In C/C++, a function must either be completely defined before it is used OR it has to have a complete prototype for the function before the function is used, so that the compiler knows in advance the number and types of any function arguments and the return type (if any) of the function itself.

Arduino generates (correctly, most of the time) a prototype for you.

econjack

Functions are the basic building blocks of C programs. Usually, a function is designed to perform one task. Many times, a function does not need any information or help from "the outside world" to perform its task. The setup() function is a case in point. It's task is to establish the environment in which the current program is run. Other functions, like sqrt() which finds the square root of a number, needs to know the number you're interested in. Therefore, its function prototype is:

Code: [Select]
double sqrt(double x);

A function prototype is nothing more that a brief description of what the function does. In the case of sqrt(), it needs to know the number of interest, x in this example, that is passed to it from "the outside world". The word double at the start of the function prototype tells you the data type that is returned from the function, a floating point double in this case.

You would use it like:

Code: [Select]
   double answer;

   answer = sqrt(4.0);


which says we want to know the square root of 4, so we pass that number to the square root function. The function does its magic, and returns 2.0 and shoves it into answer.

Note the Arduino IDE really does not support the double data type and uses a float data type instead.

Go Up