Address of function will always return true

Hello, I am new to this forum and C++. I am testing a function that will do something if another function is called or not.
Here is what I have but it is giving me an error message: Address of function will always return true

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
31
32
33
34
#include <iostream>

using namespace std;

static int c{0};

bool calfunction(int);

void displayFunc(){
    if(calfunction){
        cout << "The function was called" << endl;
    }else{
        cout << "The function was not called" << endl;
    }

}

int main(){
    calfunction(2);
    calfunction(4);
    calfunction(6);
    displayFunc();
    cout << "calfunction was called " << c << " times" << endl;
	return 0;
}

bool calfunction(int num){
    int a {num * 2};
    //cout << "a is: " << a << endl;
    int b{num * 3};
    //cout << "b is: " << b << endl;
    c++;
    return true;
}
1
2
3
void displayFunc(){
    if(calfunction){
        cout << "The function was called" << endl;
imagine that you are the computer and you read those instructions, ¿how do you interpret them?
¿do you really understand it as «check if somewhere, maybe long time ago, that function was called at least once»?
ask your classmates if they think the same.

¿do you expect to have stored a table of all the functions that were called, along with their parameters and return value? ¿what for?


or perhaps I'm misunderstanding your question
I want displayFunc() to tell me rather calfunction() function was called or not and how many time. I got the "How many times part" but I don't get why the function will always return true and how to fix it.
I am looking for something similar to
while(true){} which I have tried but I get the same result: the function will always return true.
Your global variable c is how many times it was called.
1
2
3
void displayFunc(){
    cout << "The function was called " << c << "times" << endl;
}

Yes but if the function is "not" called then the else part needs to print but if calfunction is always activated to true then the else part never happens which is my problem.

my program should be:
If calfunction is called
print it was called
else
print it was not called

But the error message says that calfunction will always be true. How do I make it false if it is not called?
So add an if/else statement of your own.

Apparently you already knew how to do that, so I wasn't going to just give you an answer to copy/paste.
Just use c in displayFunc()

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>

size_t c {};

bool calfunction(int);

void displayFunc() {
	if (c)
		std::cout << "The function was called " << c << " times\n";
	else
		std::cout << "The function was not called\n";
}

int main() {
	calfunction(2);
	calfunction(4);
	calfunction(6);
	displayFunc();
}

bool calfunction(int num) {
	int a {num * 2};
	int b {num * 3};
	++c;
	return true;
}


But note it's not good practice to use global variables.
Thank you so much. Using the c was a better approach and it worked. Thank you very much
Topic archived. No new replies allowed.