Avoid Globals For Information Passing

If you’ve run into the classic noob C programmer’s problem of “Why won’t my function update my variable?” then you might have resorted to using a global variable instead of passing the variable into the function as a parameter. Global variables allow ‘write access’ to number from both the caller and the callee, like this,

C
#include <stdio.h>

int number;

int doubleIt() {
	number *= 2;
}

int main(void) {
	number = 3;
	doubleIt();
	printf("Doubled number is %d", number);
	return 0;
}

but global variables are considered bad for lots of reasons. Not least of which is that your function can no longer double anything other than your specific number.

Instead you need to pass number into doubleIt as a pointer parameter.