Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between calling the function without parentheses and with parentheses

Tags:

flutter

dart

What is the difference between calling the function without parentheses and with parentheses on onPressed or Ontap?

I just know that void function can't be called with parentheses on onPressed.

floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: Icon(Icons.add),
  ),

_incrementCounter has void return type

void _incrementCounter() {
    setState(() {
        _counter++;
    });  
}

But I didn't find any proper documentation.

like image 380
M.ArslanKhan Avatar asked Dec 27 '19 07:12

M.ArslanKhan


People also ask

What happens when you call a function without parentheses?

When we call a function with parentheses, the function gets execute and returns the result to the callable. In another case, when we call a function without parentheses, a function reference is sent to the callable rather than executing the function itself.

What is the difference between calling function with ()?

Using a function to do a particular task any point in program is called as function call. So the difference between the function and function call is, A function is procedure to achieve a particular result while function call is using this function to achive that task.

What is the purpose of the parenthesis in the function?

Parentheses are used to enclose incidental or extra information, such as a passing comment, a minor example or addition, or a brief explanation. The writer may choose to put additional information within parentheses or to set off the text using dashes or commas.

Can I call function without parentheses in Javascript?

It means you can call a function without parentheses if that function is no parameters. otherwise you need to include parentheses if it have parameters.


3 Answers

_incrementCounter inside onPressed is a function reference, which basically means it is not executed immediately, it is executed after the user clicks on the specific widget.(callback)

_incrementCounter() is a function call and it is executed immediately.

Therefore, inside onPressed you can either pass a function reference or an anonymous function that will act as a callback.

floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: Icon(Icons.add),
  ),

or

floatingActionButton: FloatingActionButton(
    onPressed: () {
        // Add your onPressed code here!
      },
    tooltip: 'Increment',
    child: Icon(Icons.add),
  ),

The is not something specific to dart, it is also done in javascript and many other languages:

What is the difference between a function call and function reference?

Javascript function call with/without parentheses

like image 63
Peter Haddad Avatar answered Oct 17 '22 19:10

Peter Haddad


Here is the difference:

onPressed: _incrementCounter is a reference to an existing function is passed.

This only works if the parameters of the callback expected by onPressed and _incrementCounter are compatible.

onPressed: _incrementCounter() _incrementCounter() is executed and the returned result is passed to onPressed. This is a common mistake when done unintentionally when actually the intention was to pass a reference to _incrementCounter instead of calling it.

like image 3
Sunny Avatar answered Oct 17 '22 19:10

Sunny


incrementCounter is a reference to the function. You're passing the function along as a parameter to be invoked somewhere later. This is usually used as callback function, if you have a child widget

incrementCounter() will invoke the function call. For this case, your counter will automatically add 1 when the widget builds, which you don't want to happen.

And usually it is not right to call the function directly, you should write it like this:

onPressed: () {
   _incrementCounter();
},

OR

onPressed: () => _incrementCounter();
like image 1
FadhliS Avatar answered Oct 17 '22 19:10

FadhliS