Exit
  1. Learn
  2. Check
  3. Solve
  4. Reflect

Return ends everything

When return runs, the function is over. Everything after it, including the rest of any loop, never happens. That is handy when you're searching for something.

Say you want the first number that fits a rule. Walk through the numbers one by one. The moment you find one that fits, return it. There's no point checking the rest. A function that stops early is also quicker, because it does less work.

return inside a loop: ends the loop and the function together.
In code
for d in range(2, n + 1):
    if n % d == 0:
        return d

The smallest factor

A factor of n is a number that divides it evenly. The smallest factor of 35, apart from 1, is 5, because 2, 3 and 4 don't fit. Try d = 2, 3, 4 and so on. The first d that divides n evenly is the answer.

You are sure to find something, because n divides itself. So for a prime number like 7, the loop will reach 7 and return 7. No line is needed after the loop, but some languages want one anyway.

Return at the first match, not at the last one.
The smallest factor of 35
2
0
3
1
4
2
5
3
6
4
7
5
d
d
2
n
35

Try d = 2. 35 divided by 2 leaves a remainder of 1, so it doesn't divide evenly. Move on.

Move 1 of 5