Skip to main content
3 of 3
added 71 characters in body
John Wu
  • 26.9k
  • 10
  • 68
  • 92

Why is it better to use parameters instead of temporary global variables?

If you're a very old programmer like me you may have written stuff like this early on:

DIM A, B, C
LET A = 2
LET B = 2
GOSUB ADD
PRINT C
END
ADD:
LET C = A + B
RETURN

(Actually, if you're an assembly programmer, you may be stuck writing stuff like this anyway, but let's not digress.)

Of course the modern approach would be more like this:

var c = Add(2,2);
Print(c);

function Add(int a, int b)
{
    return a + b;
}

I understand the first pattern is obviously "bad;" that is not under dispute. I'm just trying to explain to another engineer the specific technical reasons why, without injecting my own opinion. A good answer will strive to be exhaustive, and stick to technical reasons, risks, and potentially cite known code smells or other authoritative sources.

John Wu
  • 26.9k
  • 10
  • 68
  • 92