Is it possible to "omit" the output parameter of a function?

 

For example, I have a defined function:

int Func(int &result)

In some cases I don't care about the "result" reference, yet I need (maybe not) to define it like this:

int res, ret;

ret = Func(res);

Is it possible to use some placeholder instead of the "result" reference? Something like this:

Func(*);
 
Petr Nosek:

For example, I have a defined function:

int Func(int &result)

In some cases I don't care about the "result" reference, yet I need (maybe not) to define it like this:

int res, ret;

ret = Func(res);

Is it possible to use some placeholder instead of the "result" reference? Something like this:

Func(*);

wrap it and call the wrapper

(if understood that you want to be able to call it without the reference) 

int function(int &result){

return(0);
}
int function(){
int notused=0;
function(notused);
return(0);
}
 
Lorentzos Roussos #:

wrap it and call the wrapper

(if understood that you want to be able to call it without the reference) 

Thanks for the reply, but wrapping is inappropriate in my particular case. More "complexity" than defining a variable (which I don't need) and inserting that variable as a reference to the function. It's not a big deal to me and I think using some placeholder instead of a function reference is not possible, but I was wondering if I just don't know about it (one is still learning). 

It's obviously something else, but in Python when there's a function that returns multiple parameters, it's possible to use "_" for parameters I don't care about. For example, if a function returns two parameters and I'm only interested in the second one, I could write:

_, second_param = Func()

 
Petr Nosek #:

Thanks for the reply, but wrapping is inappropriate in my particular case. More "complexity" than defining a variable (which I don't need) and inserting that variable as a reference to the function. It's not a big deal to me and I think using some placeholder instead of a function reference is not possible, but I was wondering if I just don't know about it (one is still learning). 

It's obviously something else, but in Python when there's a function that returns multiple parameters, it's possible to use "_" for parameters I don't care about. For example, if a function returns two parameters and I'm only interested in the second one, I could write:

_, second_param = Func()

can also do this 

#define noRefExecution(f) int L=4;f(L);

int function(int  &result){
Print("RECEIVED "+IntegerToString(result));
return(0);
}

noRefExecution(function)
 
Lorentzos Roussos #:

can also do this 

As I said. It's not important to me and I'm not looking for a workaround. I was just asking if I'd left anything out, just in case. I use this in one class and only once, so it's perfectly fine for me to define the "unnecessary" variable. Thanks anyway.

 
Petr Nosek:

For example, I have a defined function:

int Func(int &result)

In some cases I don't care about the "result" reference, yet I need (maybe not) to define it like this:

int res, ret;

ret = Func(res);

Is it possible to use some placeholder instead of the "result" reference? Something like this:

Func(*);

It's possible in some sense, but you'll not like it either because of complexity, which you mentioned before. The solution is to use an analogue of JavaScript's promise as a result - this is a pointer to a function, which your processing code can call (1) or can skip (2) in order to return (1) or not return (2) a result respectively. Since the pointers can be optional, you can define your Func so:

int /*???*/ Func(Promise ptr = NULL)
{
   if(ptr) ptr(result);
   // else don't call it
}

where the Promise is defined as:

typedef int /*???*/ ( *Promise )( int result ) ;

Then you can call Func either way - with the pointer to get result, or without a pointer, that is:

int MyPromise(int result)
{
   ...
}

...
Func(MyPromise);
...
Func();

The only thing I don't understand why do you need another - real - return int variable. This is why I makred both cases with /*???*/. In practice promises are usually returned from functions directly, not via an additional parameter.

If you need to return more than one value, consider returning a struct instead. Then you can fill only some of the fields of it, much like as in the destructuring assignment in Python with partially omitted elements.