There is a part in your program's memory called the call stack. It grows with every function call. Because these functions would call each other endlessly, your call stack would likewise grow endlessly; funnily enough though, memory is limited, hence at some point your call stack cannot grow anymore and your program will crash
In programming there’s a way of storing data called a stack. A stack is literally a stack. You can think of a stack of papers. Every time you add a paper, you add it to the top. When you remove a paper (pop) it always has to be the one on top. It operates on last in first out principles.
When I call a function, the computer needs to remember where it was, and the arguments passed to the function. If I call isodd(n) it needs to remember what is calling isodd, and the number passed to it. If I then call iseven, it needs to remember how to get back to isodd.
Now imagine we did that infinitely. Computers have a limited space to store information, so eventually the stack gets so big it begins to overflow. Since papers are never removed, we will eventually hit a ceiling.
There is no input for which either of these methods will not call the other, meaning they must call each other infinitely in an alternating pattern
since the call isn't the last thing either of them would do before returning, the computer has to store the state of the current method before entering the next, and this takes up memory
Together, these mean that any input into either of these methods would attempt to consume an infinite amount of memory, except since there isn't an infinite amount of memory to consume, it'll just be stopped when it tries to consume any more than it's allowed.
Back in the BASIC days when I first ran into the "even or odd" problem, I divided the number in question by 2, cast the result to a string, and then checked the string for a '.'
After reading this sub for a while, turns out that wasn't a half bad solution.
this gave me the idea to divide it by 2 and then test if the last character is a 5 or not
not sure if this is widely usable, but checking the last char is widespread afaik so it should be
edit: for whole numbers I have an even better idea, if you divide by 2, floor it and then multiply it by 2, you should get the same number if it's even
a&1 will be 0 for an even number, so even if there wasn't a stack overflow issue this would still not work. Well, unless that bit would never be evaluated depending how the stack overflow issue is fixed.
Wouldn't a&1 only be true if a was odd? Also, I'm not sure if those function calls would be optimized out and the compiler only looks at the other test because it knows you are trying to write isOdd() and isEven().
Why doesn't this loop forever, I'm not really new to programming, but I trip on recursion sometimes, is odds first condition goes to is even and wouldn't it go like that forever since or only checks the first condition for short circuit thing propert or condition?
That's exactly correct! The reason it can't repeat forever is because eventually it would run out of memory to remember what step it is up to and crash (in reality it would hopefully reach a compiler defined recursion limit).
Doesen't the recursive part of the function only run when the right side test is false, so the stack would never go over two calls. So shouldn't this actually run just fine?
No because the if expression is read left to right, the left side of the OR operator is evaluated first, then only if it is false does the right side need to be evaluated.
Even if this wouldn't stack overflow, the two checks are completely pointless, because if the first is true, the second would never run and if it's false, the second can't be true anymore.
Let's fix the compiler to allow this! How it would work:
Whenever there's (expr1 || expr2) or (expr1 && expr2) the compiler produces two threads to evaluate expr1 and expr2 in parallel. When one tread produces a value which makes the value of the whole expression known (so true in case of || and false in case of &&), the runtime kills the other thread and returns the value.
So in this case the infinite recursion thread would get killed as soon as one of the alternatives (like a&1) finished evaluating to true.
Woulnt the compiler evaluate the 2nd part of the expr fiest cuz its easier than a fn call? And if the 2nd part one would ACTUALLY return true, the program would not crash. Does somebody know if this behavior exists?
420
u/Maeurer Mar 03 '26
Stack overflow
155
u/Shragaz Mar 03 '26
Well, this will crash
42
u/ScootyMcTrainhat Mar 04 '26
Back in the BASIC days when I first ran into the "even or odd" problem, I divided the number in question by 2, cast the result to a string, and then checked the string for a '.'
After reading this sub for a while, turns out that wasn't a half bad solution.
40
u/Anund Mar 03 '26
a&1 will be 0 for an even number, so even if there wasn't a stack overflow issue this would still not work. Well, unless that bit would never be evaluated depending how the stack overflow issue is fixed.
15
u/Konfituren Mar 04 '26
The compiler will optimize this into a house fire
13
u/beatitmate Mar 03 '26
Will this even compile ?
15
u/LaFllamme Mar 03 '26
endless fun
10
u/WorldWorstProgrammer Mar 03 '26
I call this the "explode your compiler stack" version. Try this on Clang with any value above (+/-)512.
5
u/AccomplishedSugar490 Mar 04 '26
Fake - nobody that bent on symmetry would write ‘a&1’ in one case and ‘a%2 ==1’ in the other😂
5
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Mar 04 '26
Wouldn't
a&1only be true if a was odd? Also, I'm not sure if those function calls would be optimized out and the compiler only looks at the other test because it knows you are trying to writeisOdd()andisEven().5
u/Sephyroth2 Mar 04 '26
Why doesn't this loop forever, I'm not really new to programming, but I trip on recursion sometimes, is odds first condition goes to is even and wouldn't it go like that forever since or only checks the first condition for short circuit thing propert or condition?
9
u/Birnenmacht Mar 03 '26
funniest thing is you might get away with this in languages that have some sort of cursed breadth first evaluation order
4
u/thecratedigger_25 Mar 05 '26
Recursion. A pro gamer move so powerful that it makes the stack run out of memory and overflows thus crashing everything.
3
u/ruben_idk Mar 04 '26
Anda muy recursivo el día
3
u/shadoomslayer Mar 05 '26
Proxied recursion lmao
2
u/TheMTyne Mar 04 '26
Doesen't the recursive part of the function only run when the right side test is false, so the stack would never go over two calls. So shouldn't this actually run just fine?
2
u/dontletthestankout Mar 04 '26
I think you just spawned AGI
2
u/TitouanT Mar 04 '26
Could make it work by only removing some characters !
2
u/Dangerous_While3572 Mar 04 '26
That's a recursion without any exit conditions :) (you'll learn about that ;)
Don't you know the modulo operation?
Much simpler with that ;)
2
u/MrFrog2222 Mar 04 '26
Bro took Stack Overflow too literally
2
u/AshleyJSheridan Mar 04 '26
Thing is, npm actually has packages with these names (although only one calls its partner).
2
u/RedAndBlack1832 Mar 04 '26
.... isEven is wrong anyway bc you want to return !(a&1)
2
u/BiedermannS Mar 04 '26
Even if this wouldn't stack overflow, the two checks are completely pointless, because if the first is true, the second would never run and if it's false, the second can't be true anymore.
A working version of this can be done tho:
2
u/Terrible-Ad911 Mar 05 '26
Well that's one way to learn what the stack is real quick
2
u/Elkatra2 Mar 05 '26
return a & 1;
2
u/Key_River7180 Mar 05 '26
stacking overflows!
2
u/Throwaway229487 Mar 06 '26
Let's fix the compiler to allow this! How it would work:
Whenever there's
(expr1 || expr2)or(expr1 && expr2)the compiler produces two threads to evaluateexpr1andexpr2in parallel. When one tread produces a value which makes the value of the whole expression known (sotruein case of||andfalsein case of&&), the runtime kills the other thread and returns the value.So in this case the infinite recursion thread would get killed as soon as one of the alternatives (like
a&1) finished evaluating to true.2
u/AbdSheikho Mar 04 '26
Perfect example for dependency injection
1
u/Own_Many_7680 Mar 07 '26
Infinity stuff
1
u/v22lel Mar 07 '26
Woulnt the compiler evaluate the 2nd part of the expr fiest cuz its easier than a fn call? And if the 2nd part one would ACTUALLY return true, the program would not crash. Does somebody know if this behavior exists?
1
u/ExtensionInformal911 Mar 07 '26
I assume the correct way is something like "bool is_even(int a)=a%2"
Don't think that will work, but I can fix it based on debug logs.
1
u/Glad_Concentrate_194 Mar 04 '26
game.60fps(4k): bugs(none); if bank.Acc>1000000 return("Ez") else goon.eat.sleep(repeat)
0
u/rescuemod Mar 04 '26
Seems to be the result of AI Slop 😏