Question

I C language how do we know when to return 0; and when to return a...

I C language how do we know when to return 0; and when to return a value.Give a code example for each type and explain

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In C language, there are mainly two types of functions:

  1. Main function
  2. User defined

Main function

The return value for main indicates how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal termination is usually signaled by a non-zero return .This is the reason why void main() is explicitly prohibited by the C/C++ standard and shouldn't be used.

The valid C/C++ main signatures are:

int main()

and

int main(int argc, char* argv[])

which is equivalent to

int main(int argc, char** argv)

Hence, main should always return a value 0 and have a data type of int.

User defined functions:

In user defined functions, "return type" indicates what kind of data this function will return. In C programming, all executable code resides within a function. A function is a named block of code that performs a task and then returns control and the computed value to a caller. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task and is called in the main() function as every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

These functions may return values of any data type such as int, float, double and sometimes may not return any value and such functions have void as a data type.

Hence, these functions must return some computed value other than(may be 0 as per conditions) to the caller function according to their data type. Caller function then may store this returned value in some variable.

Example:

int square(int x)

{

   int square_of_x;

   square_of_x = x * x;

   return square_of_x;

}

OR YOU CAN WRITE THE SAME FUNCTION AS

int square(int x)  

{

   return x * x;

}

int main(void)

{

int i,s;

i=2;

s= square(i);

printf(“square of %d is %d :”,%i,%s);

return 0;

}

Here, we have declared a square function which computes square of given integer value and returns it to the caller function i e main(). We pass the value of int type to the function and store the square returned in int s. And then we print the result using printf. In main we return 0 to know if the program executed normally or not.

Add a comment
Know the answer?
Add Answer to:
I C language how do we know when to return 0; and when to return a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT