CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2010
    Posts
    4

    Change program so heading function works

    How do i make the function 'heading' work? I was thinking of deleting it, but I don't think it is the answer. Thanks a bunch.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
            int num1 = 5;
            int num2 = 10;
            cout << "The sum of num1 + num2 is " << (num1 + num2) << endl;
            return 0;
    }
    
    using namespace std;
    int heading()
    {
            cout << "name\n";
            cout << "Class\n";
            cout << "Lab #2" << endl;
            cout << "This assignment demonstrates the use of functions" << endl;
    
    }

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Change program so heading function works

    Quote Originally Posted by Hydride View Post
    How do i make the function 'heading' work?
    You'll have to be a little more specific...

    What do you expect it to do? What does it do right now?
    Old Unix programmers never die, they just mv to /dev/null

  3. #3
    Join Date
    Sep 2010
    Posts
    4

    Re: Change program so heading function works

    It only prints out "The sum of num1 + num2 is 15".

  4. #4
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Change program so heading function works

    Quote Originally Posted by Hydride View Post
    It only prints out "The sum of num1 + num2 is 15".
    I'm assuming you also want it to print out the stuff in the function 'heading'.

    To do this, you need to call 'heading' from 'main'. The code you wrote so far only defines the function 'heading', i.e. specifies what it does when it's called.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
            int num1 = 5;
            int num2 = 10;
            cout << "The sum of num1 + num2 is " << (num1 + num2) << endl;
            heading();  // call the function
            return 0;
    }
    
    int heading()
    {
            cout << "name\n";
            cout << "Class\n";
            cout << "Lab #2" << endl;
            cout << "This assignment demonstrates the use of functions" << endl;
    
    }
    There still is one problem with this code, which is that a function must be defined (or declared, see below) before (meaning "above") the place where it's first used:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int heading()
    {
            cout << "name\n";
            cout << "Class\n";
            cout << "Lab #2" << endl;
            cout << "This assignment demonstrates the use of functions" << endl;
    
    }
    
    int main()
    {
            int num1 = 5;
            int num2 = 10;
            cout << "The sum of num1 + num2 is " << (num1 + num2) << endl;
            heading();  // call the function 
            return 0;
    }
    Alternatively, you can declare the function before it's used, and define it elsewhere (e.g. at the bottom). A declaration consists of the function heading followed by a semicolon:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int heading();  // declare the function
    
    int main()
    {
            int num1 = 5;
            int num2 = 10;
            cout << "The sum of num1 + num2 is " << (num1 + num2) << endl;
            heading();  // call the function
            return 0;
    }
    
    int heading()
    {
            cout << "name\n";
            cout << "Class\n";
            cout << "Lab #2" << endl;
            cout << "This assignment demonstrates the use of functions" << endl;
    
    }
    Finally, note that the second "using namespace std;" in your original code is superfluous. You only need this once in your file, at the top.
    Old Unix programmers never die, they just mv to /dev/null

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Change program so heading function works

    To add to above comments:

    The function heading has an int return. But your code doesn't return anything and indeed it is unnecessary. So you should turn the int to void.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured