questions about void and its parameters

Aug 6, 2009 at 8:53am
hi. i'm a total newbie in C++. so far, till now i'm confused about the function of void and its parameter. can somebody teach me how does it actually function??
Aug 6, 2009 at 9:07am
Aug 7, 2009 at 2:02pm
....eventhough i'm still not very clear about its usage~~~~but anyway~~~thanks~~~~~~~~~
Aug 7, 2009 at 2:35pm
You have to be more clear on your question.

Aug 8, 2009 at 3:26am
oo ok~~~~~
..... actually how to use void~~~~~
why does is needed to be separated from the main function~~~~
that's the thing i'm confusing about~~~~~~
Aug 8, 2009 at 3:40am
What's with all the tildes?

why does is needed to be separated from the main function
I don't get this question. void is just a type (well, the absence of a type). What do you mean "separated from main()"?
Aug 8, 2009 at 3:54am
......it's something like.....


void printblank(int blank);
int main()
{
int x;
cout << "input the number of blanks: ";
cin >> x;
printblank(x);
}

void printblank(int blank)
{
for (int i=1; i<=blank; i++)
cout << " " ;
cout << "hello\n" ;
}


why does the void printblank(int blank) needed to be separated from int main()??
Aug 8, 2009 at 3:56am
Could you give an example of how it would look like if it didn't "need to be separated"?
Aug 8, 2009 at 3:58am
.......no`~~~~~ i also dont really understand the function of void~~~~~
Aug 8, 2009 at 4:54am
The code above has two functions -- main() and printblank().

main() takes no parameters and returns an int.

printblank() takes an integer as parameter and returns nothing (void).

main() calls printblank() and therefore printblank() must be declared before the usage in main(),
hence the void printblank(int blank); line immediately before int main().

The programmer of the code arbitrarily decided that s/he would create a separate function into which to place the output of leading spaces followed by "hello". There is no need to do that from a technical standpoint; the program could have worked just as well had main looked like

1
2
3
4
5
6
7
8
9
int main()
{
    int x;
    cout << "input the number of blanks: ";
    cin >> x;
    for (int i=1; i<=x; i++)
        cout << " ";
    cout << "hello\n";
}


I hope I covered all the possible meanings your question might have had....
Aug 8, 2009 at 5:22am
oo i see~~~~~
now i got it~~~~~~
thanks ya~~~~~jsmith~~~~~
Topic archived. No new replies allowed.