+ 1

Initialising function parameters

In a function definition, neither pointer or non-pointer formal parameters need to be initialised before runtime, correct? Is this standard? Can or should they be initialised anywhere in the program or is it unecessary? I read it is good practice to initialise all variables to zero or NULL if not assigning it a meaningful value straight away? Eg see comments: //No initialisation here void swap (int *num1, int *num2); int main() { int x = 25; int y = 100; printf("x is %d, y is %d\n", x, y); swap(&x, &y); // x&y both initialised before passing to swap() printf("x is %d, y is %d\n", x, y); return 0; } //num1&num2 not initialised? /**2 args are declared here when passed in but not needing initialisation? **/ void swap (int *num1, int *num2) { int temp; temp = *num1; *num1 = *num2; *num2 = temp; }

17th Jun 2019, 1:50 AM
Matt 🌏
Matt 🌏 - avatar
2 Respuestas
+ 2
Thanks swim. So given your 2nd paragraph, is it correct that the int pointer variables num1/num2 in the swap function parameter list above are global and that's why they're not explicitly needed to be initialised? Are they declared simply by defining them in the function formal parameters without listing them the usual way such as temp, x, and y are declared?
17th Jun 2019, 11:15 AM
Matt 🌏
Matt 🌏 - avatar