Pages: [1]   Go Down
Author Topic: I forgot () at the end of function call, but got no errors?  (Read 458 times)
0 Members and 1 Guest are viewing this topic.
Turku
Offline Offline
Full Member
***
Karma: 1
Posts: 224
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I forgot the () at the end of some function calls. I think that is an error, because there is nothing else with that name, but the IDE did not complain. But this happens only in if statement. Why?

Like this
      if (!Swith14)
      {
        Dacout0(maxval*4);
      }

I meant this:
      if (!Switch14())
      {
        Dacout0(maxval*4);
      }
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think that is an error, because there is nothing else with that name, but the IDE did not complain. But this happens only in if statement

if your question is calling funtion without () is error ?
No, function name represents the starting memory address of it's. e.g

Code:
void blnk()
{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
}
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
 
}

// the loop routine runs over and over again forever:
void loop() {
  blnk;  //
  Serial.println();
               // wait for a second
}

here blnk; is the case and in your case Switch14 is starting value of function
« Last Edit: November 20, 2012, 04:31:43 am by Cybernetician » Logged

From Idea To Invention

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6619
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Unfortunately, the way that the Arduino IDE drives gcc suppresses the warning messages that gcc can generate for that code.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Turku
Offline Offline
Full Member
***
Karma: 1
Posts: 224
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In Cyberneticians answer what does the blnk line do? What I remember, for me, if (!Swith14) did not work the same way as with (). I have a function which gives no result and gets no parameters. But IDE does not give an error.

Example.
This does not give an error
myfunct;

if myfunct is
void myfunct(void)
......

I cant test this now, I have to go elsewhere.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6619
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In Cyberneticians answer what does the blnk line do? What I remember, for me, if (!Swith14) did not work the same way as with (). I have a function which gives no result and gets no parameters. But IDE does not give an error.

Example.
This does not give an error
myfunct;

if myfunct is
void myfunct(void)
......

I cant test this now, I have to go elsewhere.

The line "  blnk;  //" does nothing, same as "myfunct;" in your code. These are legal but useless C++ statements, which gcc will warn about if the usual warnings (-Wall) are enabled.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int btnPressed() {
    return digitalRead(12) == LOW;
}


void setup() {
    Serial.begin(115200);
    pinMode(12, INPUT);
    digitalWrite(12, HIGH);
}


void loop() {
    if (btnPressed) {                 // missing ()
        Serial.println("Pressed");
    }
    else {
        Serial.println("Not pressed");
    }
    delay(1000);
}

A function name on its own, i.e. without parentheses, represents the function address, not its return value.
Therefore in the code above the if(btnPressed) statement will always take the "then" route, as a function address is never zero. But more importantly, it will do that regardless of the pin state.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
a function address is never zero.
...except for this very important one used in the bootloader
Code:
void (*app_start)(void) = 0x0000;
 smiley-wink
« Last Edit: November 20, 2012, 06:38:03 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

smiley-wink
Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 781
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know if this is the right way to do this or not.  I'm a completely self taught programmer.  But it works and uses the same syntax.

Most of my projects boil down to state machine code.  I'll make an enum to name all the states and keep everything in arrays I can use those names to index.  I'll register all the callbacks in an array of function pointers.  So registering a callback might look like.

Code:
void someCallback()
{   ...  some code to run   }


callback_array[SOME_STATE] = someCallback;

Then the loop function might have:

Code:
if (callback_array[current_state]) callback_array[current_state]();

and someCallback will be called if current_state == SOME_STATE where current state is typed to the enum I made to hold the state names. 

If a callback is not registered, then the array will have a NULL there and that will keep the function from being called.  It gets paired up a lot with an else statement that adds a new callback function. 
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmm... not bad for a self-taught programmer! smiley-grin

I'm using that technique too in a "app page" lib I'm working on atm...

But yes, a function pointer, like all pointers, can be NULL.
Logged

Pages: [1]   Go Up
Jump to: