Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function without parenthesis?

I was looking at the "Contose Cookbook" training for creating Windows 8.1 Apps.

Going through the first lab, I saw something like variable += function;. The function didn't have paranthesis, and as far as I can see, you can create functions without paranthesis, which are properties, but you have to call them with.

Here is a class where I found this:

namespace ContosoCookbook
{
    sealed partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        private async void OnSuspending(object sender, SuspendingEventArgs e) 
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            await SuspensionManager.SaveAsync();
            deferral.Complete();
        }

        ...
    }
}

What type of calling or function is that that accepts this.

The Application.Suspending event from msdn.

like image 207
tasegula Avatar asked Mar 14 '14 16:03

tasegula


People also ask

Can I call a function without parentheses?

Without parentheses, the function name is used as the pointer of the function. The name of a function is the pointer of the function. At this time, the result of the function is not obtained, because the function body code will not be run.

Can you call function without parentheses 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.

Do functions need parentheses?

Without parentheses you're not actually calling the function. A function name without the parentheses is a reference to the function. We don't use the parentheses in that code because we don't want the function to be called at the point where that code is encountered.

Do Python functions need parentheses?

Invoking FunctionsParentheses are necessary when you want to invoke functions. Calling on the name of a function without following it by parentheses will point towards the function object, but will not call the function itself. The code inside the body of the function will not get executed.

What is the difference between calling function with parentheses and without in JavaScript?

If I fire up my console and key in the following function (or method, depending where it is used)… Using () after a function means to execute the function and return it's value. Using no () means to fetch the function to be passed along as a callback.


1 Answers

The event handler in question takes a delegate which matches the method's arguments, allowing it to take a method group.

As a simple example, any Action variable can accept any void method with no parameters as a method group. The above event handler's delegate likely takes an object to indicate the sender and some sort of EventArguments.

The most common delegates anymore are Action<T>, Func<T> and Predicate<T>, which represent void methods, methods with a specified return type, and methods which return booleans respectively. They can also be assigned lambdas. All of them have varying numbers of parameters. They can be very useful in some situations.

Note that assigning a delegate does not invoke it; that can be done at a later time. Delegates can also be combined. It may also be worth noting that delegates can do strange things with local variables: if you assign a delegate with something like (name) => tempName += name where tempName is a local variable, your local variable will be modified on invocation of the delegate. This makes sense, but may not be obvious.

like image 132
Magus Avatar answered Sep 23 '22 23:09

Magus