-1

Why is it that any time i print a function in cpp, it prints 1. i.e

#include <iostream>
using namespace std;
void f1(){
   cout<<"called function f";
}
int f2(){
   cout<<"called f2";
   return 1;
}
int main(){
   cout << f2 <<endl; // prints 1
   cout << f1 <<endl; // still prints 1
}

Im just wondering why it prints 1.

Thanks for your help

Ekure Edem
  • 310
  • 2
  • 10

1 Answers1

1

It seems it is judging if the functions (converted to pointers to functions) are not null, returning true, and printing true as 1. This can be confirmed by adding cout << std::boolalpha; and seeing the the output is changed to true.

#include <iostream>
using namespace std;
void f1(){
   cout<<"called function f";
}
int f2(){
   cout<<"called f2";
   return 1;
}
int main(){
   cout << std::boolalpha;
   cout << f2 <<endl; // prints true
   cout << f1 <<endl; // also prints true
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70