C++ has 2 types of functions
A
value-returning function is designed to compute and return EXACTLY ONE value
using a return statement.
A
void function is a series of statements designed to perform a task. This type of
function does not have a specific data type.
What is needed to add a function to a C++ program?
function definition – consists of a heading and a body, placed after the main function
function prototype – placed after using statement and const declarations, before heading
for main function – provides information to compiler about the function
function call – placed inside any function, used when you want the statements in thefunction to be executed
1 2 3 4 5 6 7 8 9 10 11
|
//preprocessor directives
using namespace std;
//const declarations
//function prototypes
int main()
{
. . .
//function call(s)
return 0;
}
//function definitions (can include function calls)
|
So for your code.
You need to include the function prototype.
The function call in the main function.
And the function definition ( the actual computations)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
void function(int); // create a function prototype before your main function.
// the parameters are whatever data type you will send
// I changed it to an int
int main()
{
int return_value = 0; // create whatever variables you want to pass in main
function(return_value); // send "return _value" to your function
return 0;
}
void function(int n) // this is the function definition. include parameters & names
{
cout << "ENTER A NUMBER: ";
cin >> n ;
cout << n << endl;
cin.ignore(10, '\n');
cin.get();
}
|
// Your variable names can change in the function. Just to demonstrate that is why I chose "n".
//