The concept of scope in programming defines the visibility and lifespan of variables, distinguishing between global and local variables. Global variables are accessible throughout the program and last until the program ends, while local variables are confined to the function they are declared in and are destroyed once the function returns. Best practices suggest avoiding the use of global variables within functions and instead utilizing input parameters to manage data.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
16 views1 page
CH 12 Lesson 3
The concept of scope in programming defines the visibility and lifespan of variables, distinguishing between global and local variables. Global variables are accessible throughout the program and last until the program ends, while local variables are confined to the function they are declared in and are destroyed once the function returns. Best practices suggest avoiding the use of global variables within functions and instead utilizing input parameters to manage data.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
Ch 12 lesson 3
The concept of scope describes what two features of a variable?
Controls which parts of your code can see and use a variable and how long that variable lasts while your program is running. Where do you declare variables that have global scope? 1. Global variables can be used from anywhere in your program. 2. Global variables will be valid for as long as your program is running outside of any function How long do global variables last and from where can you access those variables? The variables are created when they are rst initialized with an assignment statement (=), and they will then last until your program ends. Where do you declare variables that have local scope? 1. Local variables can only be seen and used from inside that function 2. Local variables will be created when rst assigned in the function and destroyed as soon as the function returns inside a function. How long do local variables last and from where can you access those variables? Created each time your function runs, can only be seen by code inside that function, and will be destroyed when the function returns. What happens if you try to access a global variable by name inside a function? It will create a new local variable. The function statements will all use the local version, leaving the global version untouched It is possible to update a global variable from within a function, but one additional step is needed. Inside your function, add a statement that starts with the "global" keyword, followed by the name of the existing global variable. What pre x do you add to a variable name inside a function to ensure access to the globally declared variable? global Would you best describe function parameters as having local or global scope? Local variables- when you function de ne input parameters. The parameters will be destroyed when the function ends. What is best practice for thinking about global variables inside functions? Avoid using Globals inside Functions. What approach can functions use to avoid having to access global variables? That use input parameters to receive all of the data.