PDA

View Full Version : C language help, and the RETURN VALUE



newbie69
08-29-01, 03:04 AM
i need help in understanding it more, can anyone show me an example of how the return as the return 0; works and what it is for?? because my compiler was giving me warnings that i should return a value in function main or something like that, and i was like why? any help is appreciated

im a newbie in c as you can tell

Jon
08-29-01, 05:46 AM
Let me preface my reply by saying I am also very new at this , but I will try and help.

#include <iostream.h>
int main()
{
cout<<"I hope this helps";
return 0;
}

As far as I understand it, from reading a few visual c++ training books.

you are ordering main to return to 0. When a value is returned to main, it is then passed to the operating system.

I've also seen void main used, but I believe the accepted way, is to use return 0.

I hope this helps

newbie69
08-29-01, 08:42 AM
thanks for the reply, but how do you know what to put in the return value??

#include <stdio.h>

int main()
{
printf("Hello World!\n");

return 0; /* right here?? can i just put down any integer? and how will that effect the program?, if you don't return a value?, in some source codes i see people put return -1; but i don't understand why does it have to be that specific number, like what if it was an 8 instead of a -1 or even an 11, how will that effect the program?
*/
}

Stu
08-31-01, 10:05 PM
Typically the return value is used as a exit status code (I prefer to use the exit () function myself). So that, if your program is called by another program or a shell/batch script, the calling entity can react to the exit status appropriately.

To explain this a little more clearly, assume your program parses a large file and inserts the parsed data into a database. Your program is called by a user clicking a button in some other program. How does the user know if your program successfully completed the task? Or, if it was not completed successfully, why?

The easiest way to tell what happened after your program has completed is to check the returned exit status code. Most of the time these codes are defined by you. For instance, 0 if it completed okay, 1 if it could not connect to the database, 2 if it deadlocked with another user in the database, 3 if it couldn't find the file to parse, etc. Then, using these codes, the calling program can print the appropriate message to the user's screen, or use some sort of logic that will attempt to get your program to complete successfully.

bharford
09-06-01, 09:58 PM
in main you return 0.. why i am not quite sure, thats just what i've been told.

in a function what you return can be very important:
for instance
char get_char (apstring question);

would need to return a character value (char)

so the implementation (actual coding) would look something like this...

char get_char (apstring question)
{
char answer; // answer to be inputted by the user

cerr << question;
cin >> answer;

return answer;
} // get_char

the return answer part sends the character the user inputted back to a variable wherever you used the function

so...
say you have this in your main:
char letter = get_char ("what character to enter?")

when the user puts in 'P' for expample
letter would then equal P

newbie69
09-07-01, 03:59 PM
don't know much about C++, can you do that example in C? hehe sorry for the trouble