printmessage (); ?!

Hello...

Trying to learn c++ and read the functions guide on c++ tutorials here on this site. I wondered, why do you have to use parenthesis after the printmessage in the bracket of the main function.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

void printmessage ()
{
  cout << "I'm a function!";
}

int main ()
{
  printmessage ();
}
Last edited on
The parenthesis indicate that the function should be executed, and not treated as just another variable.
You can also pass parameters:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
int add(int a, int b)
{
  return (a+b);
}
int main()
{
  std::cout << add(1,2) << std::endl;
  return 0;
}
That's just the way C++ denotes a function, very common for many languages, not to mention for mathematics itself ( like f(x, y) ). It's relatively easy to parse because you know it has to be a function if it's a delimiting set of parentheses after a name.

Also, if your function actually had arguments that were passed into it, then that's where those arguments would go. ex: abs(-3)

If you want to get more technical, the name of the function itself is a pointer to the function, just as the name of an array is the pointer to that array. Don't worry about that if you haven't learned about pointers yet.
Last edited on
Topic archived. No new replies allowed.