In general, "return" will leave a function; skipping what ever code is executing.
Lets say you have a simple pseudocode structure like this:
Code:
function DoMyStuff(coolnumber) {
if (coolnumber < 5) {
print "Cool Number is too small"
return
}
print "Cool Number is :" + coolnumber
}
The return inside will directly leave the function. The second print is not executed.
For recursions, there is something to keep in mind:
Each function call will use some memory on the stack, for local variables, for parameters and the return. This is true for most (if not all) compiled languages, and also some script languages.
Code:
function MyStuff() {
print "Hello World!"
MyStuff() // <- recursive call
}
With each call to MyStuff, the stack ascends by at least the return address. If we run that program, it will run forever. But sadly, the size of the stack is limited. With each recursion, the stack gets fuller and fuller because the function never returns. And at some point, there is no memory left -> Stack overflow
Lets look at a different example: A formular for the greatest common divisor of two numbers based on Euklid.
Code:
function gcd(a, b) {
if (b == 0) {
return a
}
return gcd(b, a % b)
}
The recursion goes on and on, but at some point, b will become 0. If this occurs, the function will return without calling itself again. At this point, the recursion stops. The program descends through the stack.
There is still a chance that this function will overflow the stack. But unlike the first example, which had a 100% chance to overflow the stack, this one will only fail for really large numbers of recursions, aka. really large numbers as input.