The life of variables in functions

We have discussed the life of variables before… but does it change when we add additional functions? Take for example, the following snippet of code:

1    double is_odd(int numI)
2    {
3        if (numI%2 == 1)
4            return 1;
5        else
6            return 0;
7    }
9    int main(void)
10   {
11       int odd, numO=2;
12       odd = is_odd(numO);
13       return 0;
14   }

The scope of the variable numO in the main program is the main program (lines 11-13), whilst the scope of the variable numI in the function is_odd is inside the function (lines 1-6). When the function is_odd is invoked, the variable numI is created to store the incoming number to check. When the function is destroyed, so to is numI. These two variables exist in different realities. We cannot access numO within is_odd, nor can you access numI in the main program.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s