Why does following code has a correct output? int GGT has no return statement, but the code does work anyway? There are no global variables set.
#include <stdio.h> #include <stdlib.h> int GGT(int, int); void main() { int x1, x2; printf("Bitte geben Sie zwei Zahlen ein: \n"); scanf("%d", &x1); scanf("%d", &x2); printf("GGT ist: %d\n", GGT(x1, x2)); system("Pause"); } int GGT(int x1, int x2) { while(x1 != x2) { if(x1 > x2) { /*return*/ x1 = x1 - x2; } else { /*return*/ x2 = x2 - x1; } } }
A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non- void return type, the compiler issues a warning message.
Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.
Answer. NO, a function does not always have to have an explicit return statement. If the function doesn't need to provide any results to the calling point, then the return is not needed. However, there will be a value of None which is implicitly returned by Python.
nothing, the return value gets ignored.
For x86 at least, the return value of this function should be in eax
register. Anything that was there will be considered to be the return value by the caller.
Because eax
is used as return register, it is often used as "scratch" register by callee, because it does not need to be preserved. This means that it's very possible that it will be used as any of local variables. Because both of them are equal at the end, it's more probable that the correct value will be left in eax
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With