Can return without a value be used in any point of a void function as a kind of goto to the main function ? If so, can there be an unlimted number of returns?
Printable View
Can return without a value be used in any point of a void function as a kind of goto to the main function ? If so, can there be an unlimted number of returns?
Yes, you can use return by itself to return from a function to the function that called it. There can be as many returns as you want. I assume you are talking about using ifs and then return from each one. For example:
- SeanCode:void Function(void){
if(x==4){
//Do something
return;
}
else if(x<4){
//do something else
return;
}
//do something here and end the function
}
>There can be as many returns as you want.
Yes, but realise that when a function has too much exit points, it may get very hard to maintain and read the function. In such cases you could consider to write a new function to handle specific stuff. It is good programming practice to let a function have just one exit points. But sometimes it is more efficient to have more exit points, for example to prevent the function from having a lot of nested if-statements.