Calling A Function Multiple Times

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;
int count=1;
void Test();
   
int main()
{   
    Test();    
    Test();     
    Test();

    
    
    system("PAUSE");
    return 0;
}
void Test()
{
while(::count<=5)
{
cout<<::count;
::count++;
}
}


I'm trying to get this program to call the function 3 times, so the output will say "12345 12345 12345" but for some reason everytime I run it, it only prints once on the screen. Does anyone know why that is?
Last edited on
you forget to set count back to 1 after the function call
Last edited on
You are successfully calling the function multiple times.

The problem is 'count' is global (when it really shouldn't be) and thus it's value is being remembered across function calls. IE:

1
2
3
4
5
int main()
{
    Test();  // this prints what you expect, and now count == 5
    Test();  // because count == 5, this doesn't print anything (the loop is skipped)
    Test();  // ditto 


I'll try not to lecture you on the evils of globals, but try making this variable local to Test, rather than making it global:

1
2
3
4
5
6
7
8
9
10
11
// don't forget to erase the global 'count'

void Test()
{
    int count = 0;
    while(count <= 5)
    {
        cout << count;
        ++count;
    }
}
Thank you for your replies. I figured it out from Mathes suggestion, but thank you too Disch for yours. Both make more since to me. I have one other question: If I had declared "count" in the main function instead, how would I do something like pass by value or pass by reference? Do you think one functions better than the other?

I'm trying to pass by reference here, but it's not running correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

void Test(int& k)
{
while(k<=5)
{
cout<<k<<" ";
k++;
}

int main(void)
{   
int count=1;       

Test(count);
Test(count);
Test(count);
    
    
    
    system("PAUSE");
    return 0;
}
}
Last edited on
pass by value:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Test(int c);

int main()
{
    int count = 1;
    Test(count);    
    Test(count);     
    Test(count);
    
    return 0;
}

void Test(int c)
{
	while(c<=5)
	{
		cout<<c++;
	}

	return;
}


by reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;
void Test(int &c);
   
int main()
{
	int count = 1;
    Test(count);
    count = 1;
    Test(count);
    count = 1;
    Test(count);
    
    return 0;
}

void Test(int &c)
{
	while(c<=5)
	{
		cout<<c++;
	}

	return;
}


so you see, when you call it by reference, you have to reset count after the function call (just like you had to, when count was global). here you should use calling by value. because count will still be 1 after Test returned.
calling by reference makes sense if you want to modify the argument, like a function to swap to integers e.g..


edit:
you see, what you did up there is just the same error that you had using the global count. you forget to reset count ;)
Last edited on
Thank you! That makes so much more since! The only problem I have now is that when I at least try to call the function 3 times from those states, it only prints out a single "12345." I figured since you reset count back to one, it would print out again, but it doesn't. Any idea why? Possibly it's something to do with the while statement?
hmm my code works for me, and yours does too, if i modify it a little bit (resetting count and moving the } from line 26 to 12).
so i think post the code your are using right now, so we can take a look at it
pass by value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;
void Test(int c);

int main()
{
    int count = 1;
    Test(count);
    count=1;    
    Test(count);
    count=1;     
    Test(count);
    count=1;
    
    return 0;
}

void Test(int c)
{
	while(c<=5)
	{
		cout<<c<<" ";
		c++;
	}
    
    system("PAUSE");
	return;
}


pass by reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;
void Test(int &c);
   
int main()
{
	int count = 1;
    Test(count);
    count = 1;
    Test(count);
    count = 1;
    Test(count);
    
    return 0;
}

void Test(int &c)
{
	while(c<=5)
	{
		cout<<c<<" ";
        c++;
	}

    system ("PAUSE");
	return;
}


They both only seem to read out one set of "1 2 3 4 5", but since the function is called 3 times, I figured it would be displayed 3 times, unless i'm missing something.
remove system("PAUSE"); from the function. i think that should help ;-)
Hmmmm...I'm not sure where exactly to input it, because without that, the .exe box just closes :P
put it right before return 0; in main.
now, when test runs it will print 1 2 3 4 5 and will call system, so your program stops to run.
Last edited on
You saved the day! :) Thank you for all your help. It's so nice of you to help a struggling student like myself. I have two more questions. Sorry for so many :P

Isn't a local variable the exact same as declaring it anywhere in the main function?

And isn't a static variable the same as declaring a global variable?
you're welcome. and dont worry about asking, that is what this forum is for :D

to your first question:
no.
1
2
3
4
5
6
7
8
9
int main() {
  int i = 0;
  if(i==0) {
    //x is local in here
    int x=3;
  }
  //here x is unknown, because it just exists locally in the {} of if
  return 0;
}

see, a variable is only "visible" between the "nearest" { and } it is declared in. (except for global variables)
the main function is nothing special. a variable declared there is not "mightier" then any other local variable in another function.
main is just the starting point of your program. variables tehre are only known in main, not in other functions (thats why we have call by reference and call by value ;-) )
(i hope you understand what i am trying to say, my english isnt very good if i have to explain things :D )

now to the second... thats kinda right. a global and a global static variable are the same, there is no difference.
but the difference can be seen here
1
2
3
4
5
void something() {
  static int counter = 0;
  cout << "The function has been called " << counter++ << " times.\n";
  return;
}

here you just can access the variable counter in the function, not in any other place of you code. but unlike a "normal" variable counter will remain even if the function returned. you could use a global variable for that too, but there is the problem, that it can be modified anywhere in the code. that is not safe. in the above example it would hurt if counter was global and somewhere changed. but i think you understand what i mean.
Last edited on
Topic archived. No new replies allowed.