Christoffer
Christoffer

Reputation: 77

Calling a function in C++

I'm new to the whole C++ programming thing, and I know this is a easy solution, but I just cant figure it out! I simply just wanna call a function that prints out 1 + 4. Here's the code:

#include <iostream>
using namespace std;

int func()
{
    cout << 1 + 4;
    return 0;
}

int main()
{
    int func();
}

It shows nothing in the console window, only that the application stopped with return code 0. Can someone tell me what's wrong?

Upvotes: 2

Views: 130

Answers (2)

Muhammad Danish
Muhammad Danish

Reputation: 109

you can just call the function by its name. Like func();

int func()
{
    cout << 1 + 4;
    return 0;
}

the above function is retruning an integer. you are returning 0. to make it more useful return the sum and catch it in main function.

int func(){
    return 1+4;// return 5 to main function.
}

now in main.

int main (){
     int ans = func();// ans will catch the result which is return by the func();
     cout<<ans;
     return 0;
}

try to understand the working of each statement.

Upvotes: 1

billz
billz

Reputation: 45410

You are not calling func() function correctly:

int main()
{
    // int func(); This line tries to declare a function which return int type.
    //             it doesn't call func()
    func();   // this line calls func() function, it will ouput 5
    return 0;
}

Upvotes: 6

Related Questions