CAS CS 111 Lecture Notes - Lecture 6: Global Variable, Local Variable

170 views19 pages
Verified Note

Document Summary

The scoop of a variable is the portion of your program in which the variable can be used. When we assign a value to a variable inside a function, we create a local variable. It can"t be accessed outside of that function. The parameters of a function are also limited to that function. When we assign a vale to variable outside of a function, we create a global variable. A global variable can be used anywhere in your program. In code that is outside of any function. In code inside a function (but this is not recommended) Neither globals nor locals exist until they are assigned a value! The program above has two different variables called b. When this happens, the local variable has priority inside the function to which it belongs. Question: what is the output of this code? (version 2) It is not a good idea to access a global variable inside a function.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents