Search code examples
c++xcode4

Problem when executing a function


I'm trying to run a simple C++ program (as I'm JUST starting to learn C++) and tried this example code off a website as a program that adds two numbers together. When I execute the program, I never get errors, but c always returns as 0. Help??

#include <iostream.h>

int Add (int x, int y)
{

    std::cout << "In Add(), received " << x << " and " << y << "\n";
    return 0;
}

int main()
{
    std::cout << "I'm in main()!\n";
    int a, b, c;
    std::cout << "Enter two numbers here: ";
    cin >> a;
    cin >> b;
    std::cout << "\nCalling Add()\n";
    c=Add(a,b);
    std::cout << "\nBack in main().\n";
    std::cout << "c was set to " << c;
    std::cout << "\nExiting...\n\n";
    return 0;
}

Thanks in advance.


Solution

  • Since Add returns 0, c will always be 0. You need to, you know, actually add the numbers you pass into Add and return that from the function.