   About C basics....!
  Hallo every body. I am a beginer in the field of programing and hence asking here a very basic problem in C language. Please forgive me if this question looks you to be stupid. Here is my problem...Any function returns a value to another function which have called it, eg. Look at the following simple program-main(){int a, fact;printf("\nEnter any number");scanf("%d",&a);fact= factorial(a);printf("Factorial Value= %d", fact);}factorial(int x){int f=1, i;for(i=x;i>=1;i--)f= f*i;return(f);}In this program, We have used a user defined function 'factorial'. The statement 'return(f)' tells the compiler to return the value of variable 'f' to the function 'main'.'main' is also a function, which also must return a value to something. This is my question that to whom the function 'main' returns value. If it is the O/S, then what the O/S does with this value? Further, we use a keyword 'void' before the function 'main' to tell the compiler to not to return any value by the function 'main'. What does this all mean???   
  Moved to the For Beginners forum.   ;  Thanks....!   ;  Hi !A couple of remarks:- get rid of extra brackets and just write "return f;" it's fine and what almost every programmers do.- void means that the function returns no value. I'm not so aware of the internals, but to you it's just that: there is no ouput, niet, nada :) So you should put a void before your main (because it returns nothing) and an int before your factorial because it returns an int. I think C language assume in int by default but this is not really a good coding style. You better explicitly write the returned parameter type.- be aware that the max value your function should recieve is 12, because 13! > 2^31 and a 32bit integer can be at most equal to 2^31 (about 2,000,000,000)Since factorials are only positive number, you can turn your int into an unsigned int (but that will not help since 12! = 480,000,000 and 13! = 6,000,000,000 and the max for a unsigned is 2^32 = 4,000,000,000) Bear in mind that numbers are not infinite on a computer.- what value is returned by main is used by the OS to detemine whether the program ended because of an error (which case the returned value is non-zero) or because it was it's normal behaviour (return 0). What the OS does depending on that value is IMO up to it and the last thing you should care about unless you're targetting some advanced stuff or are someone very curious (which is very fine ^^). In that case you might want to ask you friend google or wait for some expert (which I'm not :p) to explain us here :)Thoses are the most basic things I think I should tell you. of course there would be more to say (but there is always more to say anyway)Personnal opinion: If you're new to programming, then take a step further at learn C++ instead of C. I think You would get a more satisfying programming experience with C++ which I find clearer. Plus object oriented is somehow a more natural approach of programmingBest regards and good luckjanta.[Edited by - janta on May 28, 2006 2:15:40 AM];  As janta already said - function main, returns a success code to the OS.However, i would like to add that, to avoid confusion - you should always specify return type for your functions, like this:int factorial(int x);int main(){  int a, fact;  printf("\nEnter any number");  scanf("%d",&a);  fact= factorial(a);  printf("Factorial Value= %d", fact);  return 0;}int factorial(int x){  int f=1, i;  for(i=x;i>=1;i--)    f= f*i;  return f;}Oh, and one more thing - when you post some code to these forums, put the code into source tag ([ source][/source ]).   ;  The return value of main can be fetched after the program has exited.This can be done from another program, or from a batch script for example.Say you write a startup script to send you the boot messages on system startup......sendmail me@mail.com `dmesg`if $?  echo "sendmail returned exit code $?. Exiting..."  exit 1else  echo "sendmail finished successfully"endif...Something like that.This is a linux example, but win32 batch programming has similar functionality.$? is a system variable that always hold the return value from the last program that exited.As you can see, the return value from main is very useful in scripts =)Also note that common convention has it that a program should return zero (0) if it was succsessful, and some other value for errors   ;  shouldn't this functionint factorial(int x){  int f=1, i;  for(i=x;i>=1;i--)    f= f*i;  return f;}look like this:int factorial(int x){  int f=1, i;  for(i=x;i>=1;i--)    f+= f*i;  return f;};// main is the function that is first called when the program startsint main(){  // since we only want positive numbers, we use an unsigned int type  // here - we can skip the "int" as it is implicit.  unsigned a;  printf("\nEnter any positive number");  // we change to %u for unsigned number, rather than %d which is signed  scanf("%u", &a);  // we don't need to store the value of factorial if we're just printing  // the result to the user. We can use the return value straight away.  printf("Factorial value: %u", factorial(a));  // if this isn't defined, then just return 0, I believe EXIT_SUCCESS  // is just zero anyway.  return EXIT_SUCCESS}unsigned factorial(unsigned x){  unsigned f, i;    f = 1;  // we're allowed to write the entire loop out on one line if we want  // to. also, notice how we've typed --i rather than i--: this is  // probably not necessary, but if you're using a bad compiler that  // doesn't support that sort of optimisation (that gets rid of the  // temporary value generated by use the -- after the variable), then  // it'll save time and space.  //  // furthermore - we're actually writing out the factorial bit correctly,  // :D. since we don't need to multiply by one, we don't need >=, but  // rather just >. Also, we're using *=, which makes f equal f times i,  // just like factorial.  for (i = x; i > 1; --i) f *= i;  // return value  return f;}