Solution for Exercise 10-5

ex1005

#include <stdio.h>

void vegas(void);

int main()
{
    int a;

    a = 365;
    printf("In the main function, a=%d\n",a);
    vegas();
    printf("In the main function, a=%d\n",a);
    return(0);
}

void vegas(void)
{
    int a;

    a = -10;
    printf("In the vegas function, a=%d\n",a);
}

Notes

* Variables inside one function don't affect variables inside another, even when given the same names.

* You don't need to skimp on variables in a function. If the function needs a dozen variables to do its thing (to funct), declare a dozen variables.

* I tend to re-use short variable names more than I do long ones. For example, I often use x in a for loop or ch when reading a character. Those variable names are used in functions and their contents are always considered local. I dub more important variables with specific names, such as status or current_line or total. Even when such variables are used only in a function, I don't re-use their names anywhere else in the code. It's all personal preference, really.

* While I use the term local variable to refer to variables used in a function, it's not an official C language term. All variables in C are local because they are all used within a function. The exception is the external or global variable, which is covered in Chapter 16.