Yash Jaiswal
Yash Jaiswal

Reputation: 149

Programming error in C++

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;
}

Upvotes: 3

Views: 170

Answers (6)

IndieTech Solutions
IndieTech Solutions

Reputation: 2541

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

Upvotes: 4

alisa
alisa

Reputation: 11

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;
}

Upvotes: 0

Sunder R
Sunder R

Reputation: 1094

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;
}

Upvotes: 6

Callie J
Callie J

Reputation: 31296

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();

Upvotes: 4

rpsml
rpsml

Reputation: 1498

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.

Upvotes: 8

Mark Segal
Mark Segal

Reputation: 5550

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.

Upvotes: 0

Related Questions