asked 51.3k views
4 votes
Write the definition of a function named counter that receives no parameters and returns 0 the first time it is invoked, returns 1 the next time it is invoked, then 2, 3 and so on.

1 Answer

5 votes

Answer:

int counter( )

{

static int xvalue = 0;

return(xvalue++);

}

Step-by-step explanation:

The first line of code declares the function and its return type integer. Then we create a static variable xvalue and initialize to 0. the statement return(xvalue++) ensures that at each method call, the value of integer is increased by 1

answered
User Poovizhirajan N
by
7.9k points