|
-
September 14th, 2010, 08:03 PM
#1
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;
}
-
September 14th, 2010, 08:23 PM
#2
Re: Change program so heading function works
 Originally Posted by Hydride
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
-
September 14th, 2010, 08:57 PM
#3
Re: Change program so heading function works
It only prints out "The sum of num1 + num2 is 15".
-
September 14th, 2010, 09:23 PM
#4
Re: Change program so heading function works
 Originally Posted by Hydride
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
-
September 15th, 2010, 03:25 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|