Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

functions

0 views
Skip to first unread message

arunix

unread,
Sep 1, 2009, 1:58:28 AM9/1/09
to
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......

Tim Love

unread,
Sep 1, 2009, 2:13:44 AM9/1/09
to
arunix <arr...@gmail.com> writes:

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

This line doesn't call IsOdd. IsOdd will be called later by the count_if code, and when it is, it will be called with the appropriate parameter.

Alf P. Steinbach

unread,
Sep 1, 2009, 2:14:07 AM9/1/09
to
* 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

arunix

unread,
Sep 1, 2009, 2:37:12 AM9/1/09
to

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

arunix

unread,
Sep 1, 2009, 2:39:14 AM9/1/09
to
On Sep 1, 11:14 am, "Alf P. Steinbach" <al...@start.no> wrote:

Thanks

Juha Nieminen

unread,
Sep 1, 2009, 11:03:24 AM9/1/09
to

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

0 new messages