Go Down

Topic: I forgot () at the end of function call, but got no errors? (Read 497 times) previous topic - next topic

LMI

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);
      }

Cybernetician

#1
Nov 20, 2012, 10:28 am Last Edit: Nov 20, 2012, 10:31 am by Cybernetician Reason: 1
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: [Select]

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
From Idea To Invention

dc42

Unfortunately, the way that the Arduino IDE drives gcc suppresses the warning messages that gcc can generate for that code.
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.

LMI

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.

dc42


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.
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.

tuxduino

Code: [Select]

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.

AWOL

#6
Nov 20, 2012, 12:28 pm Last Edit: Nov 20, 2012, 12:38 pm by AWOL Reason: 1
Quote
a function address is never zero.
...except for this very important one used in the bootloader
Code: [Select]
void (*app_start)(void) = 0x0000;  ;)
"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.

tuxduino


Delta_G

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: [Select]

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


callback_array[SOME_STATE] = someCallback;


Then the loop function might have:

Code: [Select]

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. 

tuxduino

Hmmm... not bad for a self-taught programmer! :D

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.

Go Up