3

I recently started learning C++ but I came across a problem. The program given below is not giving me the desired result as I only see 'Hi' in the result but not what's written in the void function. Please tell me the reason that this is happening along with the solution.

I am using Xcode 6.3.1 and the I have selected the language C++.

#include <iostream>
using namespace std;

void ABC () {
    cout << "Hey there ! \n";
}

int main () {

    cout << "Hi \n";

    void ABC ();

    return 0;
}
1
  • 5
    void ABC(); is a function declaration, not invocation. To invoke, write ABC();. Commented May 16, 2015 at 17:26

6 Answers 6

8

You are redeclaring a void ABC() function inside main(). Just call ABC(); without the void.

You can take a look at this question about declaring a function within the scope of another.

Sign up to request clarification or add additional context in comments.

Comments

6

In your code your function call was wrong.

When you call your function you don't need to add the return type:

#include

void ABC () {

  cout << "Hey there ! \n";

}

int main () {

  cout << "Hi \n";

  ABC ();

  return 0;
}

Comments

4

you need to call your method and not declare it inside main

#include <iostream>
using namespace std;
void ABC () {
    cout << "Hey there ! \n";
    }
     int main () 
    {
         cout << "Hi \n";
        ABC ();
         return 0;
        }

EDIT 1: Since you started learning C++ i recommend the following recommendations to make sure your code is cleaner. Please note , these are not rules by any mean , but more of best practices and a style of coding.

  • Use meaningful names for your variables, methods, functions , classes ... So instead of ABC() name it something that if you (or someone else is reading it) will now what it suppose to do.
  • When calling methods and functions try to declare them with the appropriate returning value. Void by definition doesn't return any value it just process the code inside of it. so your methods/function should return appropriate values of do what it suppose to.

Here's version 2 of your code with examples of 3 different methods and calls:

#include <iostream>

using namespace std;
int sum;
string  MethodReturningString() 
{
    return "Hey there i am the result of a method call !";
}

int  MethodReturningInt() 
{
    return 5;
}

void CalculateSum(int x,int y)
{
  sum=x+y;
}
int main()
{
   cout << MethodReturningString()  << endl; 
   cout << MethodReturningInt()  << endl; 
   cout << "Calculating sum:" ; 
   CalculateSum(5,4);
   cout << sum << endl; 
   return 0;
} 

Happy coding

Comments

4

In C++, like pretty much any other language, you do not specify the return type when calling a function. So change the line that reads:

void ABC ();

to:

ABC();

Comments

0

Try this:

#include <iostream>
using namespace std;

void ABC () {

cout << "Hey there ! \n";
}

 int main () {

 cout << "Hi \n";

 ABC();

 return 0;
}

You should call a function simply by stating its name and adding parentheses.

Comments

0

instead of using void ABC() for calling the function ABC() in main(), use the following code:

#include       

void ABC ()
{
     cout << "Hey there ! \n";
}

int main ()
{
        cout << "Hi \n";
    ABC ();
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.