
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.