Hi,
It seems you also have some fundamental misunderstandings about how functions are declared, defined, and called. Here is a simple example:
Code:
#include <iostream>
using namespace std;
void display(int n);
int main()
{
int number = 10;
display(number); //calls the display() function
return 0;
}
void display(int n)
{
cout<<n<<endl;
}
Some things to note:
1) The first line in blue is the function 'prototype' or the function 'declaration'. It is placed outside of main() and before main().
2)In main() the function is called with this line:
display(number);
Notice that when you call a function, you use paretheses after the function name, and inside the parentheses, you have to include the values the function is expecting. In this case, the variable 'number' is listed inside the parentheses. That is an instruction to fetch the value stored in the variable 'number' and send it on to the function. Notice also that you do not send a type to a function. You wouldn't do this:
display(int); //error
After the value is sent to the function, the value is stored in what's called the function 'parameter'. The function parameter for the display() function is 'n':
Code:
void display(int n)
{
cout<<n<<endl;
}
It's type is an int, so whatever value is sent to the function has to be type int. The int value that is sent to the function is then stored in the variable 'n'. Inside the function, the variable 'n' can be used to obtain that int value:
cout<<n<<endl;
Finally, when a function returns a value(which is not the case with the display() function), the function call in main() is replaced by the return value.
3) The function definition is placed outside of main() and after main(), and it must list the function parameter by name---not just by type which is allowed in the function declaration.
Another comment on scope: generally variables are only valid inside the 'block' they are declared in. A 'block' is formed by a set of braces:
Code:
int main()
{
int num;
string str;
return 0;
}
So, if you need the values of the variables in a function, you must pass them to the function:
Code:
int main()
{
int num;
string str;
myFunc(num, str);
return 0;
}