functions

A

arunix

here is mine question aout the "functions" function return a value.
if we have parameterized function then we have to pass argument to the
function but i was learing from a website "/www.cplusplus.com/".

here is a function

bool IsOdd (int i) { return ((i%2)==1); }

and they used it in the count_if but with no arguments...

mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
its Grt Confusion......
 
A

Alf P. Steinbach

* arunix:
here is mine question aout the "functions" function return a value.
if we have parameterized function then we have to pass argument to the
function but i was learing from a website "/www.cplusplus.com/".

here is a function

bool IsOdd (int i) { return ((i%2)==1); }

and they used it in the count_if but with no arguments...

mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
its Grt Confusion......

You have to pass an argument to the function when you call it.

The 'count_if' invocation does not call the function directly.

It passes the function itself as an argument.


Cheers & hth.,

- Alf
 
A

arunix

* arunix:






You have to pass an argument to the function when you call it.

The 'count_if' invocation does not call the function directly.

It passes the function itself as an argument.

Cheers & hth.,

- Alf

it means Automatic argument passing by the count_if function
it will not work is i use it as IsOdd
?
 
A

arunix

* arunix:






You have to pass an argument to the function when you call it.

The 'count_if' invocation does not call the function directly.

It passes the function itself as an argument.

Cheers & hth.,

- Alf

Thanks
 
J

Juha Nieminen

arunix said:
here is mine question aout the "functions" function return a value.
if we have parameterized function then we have to pass argument to the
function but i was learing from a website "/www.cplusplus.com/".

here is a function

bool IsOdd (int i) { return ((i%2)==1); }

and they used it in the count_if but with no arguments...

mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
its Grt Confusion......

Maybe this will clear things out:

template<typename Function>
void callThisFunction(Function f)
{
f(1);
}

void someFunction(int i)
{
std::cout << i << std::endl;
}

int main()
{
callThisFunction(someFunction);
}

(The interesting (and a bit complicated) part is what 'Function' in
that template resolves to. In this particular case you don't need to
worry about that, though.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top