Question:
{} curly bracket should have a return type?
Vicky
2014-04-06 06:29:32 UTC
curly bracket issue in c programming
}
they said this bracket should have a return value
Three answers:
?
2014-04-06 06:52:07 UTC
Sweet Danger got it right so he deserves the points for best answer.



Just to expand a little: In C, when you write something like:



int do_something () {....}



then you're saying that do_something is a routine that will do some work and give you an answer back - that answer will be an int value. (You can make it any other type by changing int to float or double or whatever).



Now, if you write:

int x;

x = do_something ();



then do_something () executes its statements and "returns" an answer. That answer gets put into x. However, the computer isn't psychic so you have to tell the computer exactly what answer you want it to return. Hence the return statements, such as: return 0; or return 2*x+y; or whatever. Nearly always, the return statement should be the very last statement in the routine. The beauty of this is that we can use (or "call") do_something() multiple times, but even if we only call it once then using still makes our code look a lot tidier.



Sweet Danger also mentioned void. If you want a routine to do something but not give an explicit answer back, then you can use "void" instead of int, double, etc. When you do this, you should also NOT put in a return statement (as there's nothing to return). Now, when you want the routine to do something, you simply write its name, followed always by the ().
?
2014-04-06 06:36:34 UTC
well, it's not like I'm an expert but you need a return value. Like if your using

int a () {

// return 0; or

return a;

}



well you will need to return an integer value. This stands also for double and float. The only time u don't need a return value is when u use void. :D

Well I hope I helped in any way hehe. I only started programming last year (and this year I'm studying java :P) Hopefully my explaining was good enough haha :D
Iustin
2014-04-06 06:58:10 UTC
If you have a function of a type other than Void, you will have to return a value of that type.



Example:



float function (float a) {

float b;

b=a/2.0;

return b;

}



If you do not return anything you will get an error from the compiler. The next subprogram does the same thing but doesn't need to return something because it has a Void type and it prints the output onto the screen:



void function (float a) {

float b;

b=a/2.0;

printf("%f", b);

}



Hope it helps and happy programming !


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...