Contenido del Curso
C++ Functions
2. Function Arguments Specification
3. Function Return Values Specification
4. Some Advanced Topics
C++ Functions
Variable Scopes
Variable scope in C++ refers to the region or context within a program where a particular variable is visible and accessible. In other words, it defines where you can use a variable in your code and determines its lifetime.
Variables declared within a function have local scope. They are accessible only within the function or the block of code inside which they were declared. Local variables are created when the program enters the block where they are defined and destroyed when the block is exited.
Note
In C++, a code block is a set of statements enclosed within curly braces
{ }
.
Let's look at the following example:
main.cpp
We can see the following error message: error: ‘localVar’ was not declared in this scope
.
The variable localVar
was created inside the MyFunction()
function and was destroyed after its execution. As a result, in the main()
function, we are trying to access a non-existent variable and getting an error.
To use the value of localVar
we have to assign the return value of a function to a variable created inside the main
function:
main.cpp
You may ask a completely logical question: How can we return a variable from a function if this variable is destroyed after the function completes?
Concept | Description | Analogy |
---|---|---|
Local Variable Allocation | Memory is allocated for local variables when a function is called | Reading a book in the library; the book can't be taken outside, but you can remember its content |
Returning a Value | A function can return a value using the return statement |
Remembering the content of a book after reading it in the library |
Storage for Return Value | The returned value is temporarily stored in memory | Temporary memory storage, similar to remembering the book's content |
Capture in Calling Function | The returned value is captured by assigning it to a variable | Writing down the book's content in a notebook after leaving the library |
Copying the Value | The value is copied from temporary storage to a new variable | Copying the book's content from your notebook to a new sheet of paper |
Use of the Captured Value | The new variable can be used as if it holds the original value | Reading the copied book at home |
¿Todo estuvo claro?
Contenido del Curso
C++ Functions
2. Function Arguments Specification
3. Function Return Values Specification
4. Some Advanced Topics
C++ Functions
Variable Scopes
Variable scope in C++ refers to the region or context within a program where a particular variable is visible and accessible. In other words, it defines where you can use a variable in your code and determines its lifetime.
Variables declared within a function have local scope. They are accessible only within the function or the block of code inside which they were declared. Local variables are created when the program enters the block where they are defined and destroyed when the block is exited.
Note
In C++, a code block is a set of statements enclosed within curly braces
{ }
.
Let's look at the following example:
main.cpp
We can see the following error message: error: ‘localVar’ was not declared in this scope
.
The variable localVar
was created inside the MyFunction()
function and was destroyed after its execution. As a result, in the main()
function, we are trying to access a non-existent variable and getting an error.
To use the value of localVar
we have to assign the return value of a function to a variable created inside the main
function:
main.cpp
You may ask a completely logical question: How can we return a variable from a function if this variable is destroyed after the function completes?
Concept | Description | Analogy |
---|---|---|
Local Variable Allocation | Memory is allocated for local variables when a function is called | Reading a book in the library; the book can't be taken outside, but you can remember its content |
Returning a Value | A function can return a value using the return statement |
Remembering the content of a book after reading it in the library |
Storage for Return Value | The returned value is temporarily stored in memory | Temporary memory storage, similar to remembering the book's content |
Capture in Calling Function | The returned value is captured by assigning it to a variable | Writing down the book's content in a notebook after leaving the library |
Copying the Value | The value is copied from temporary storage to a new variable | Copying the book's content from your notebook to a new sheet of paper |
Use of the Captured Value | The new variable can be used as if it holds the original value | Reading the copied book at home |
¿Todo estuvo claro?