Local Variables
Anonymous contributor
Published Nov 12, 2024
Contribute to Docs
A local variable is a variable whose scope is limited to the function or block where it is defined. It exists only within that function and can only be accessed from there.
Syntax
void local_variable(){
int a_local_variable;
}
The variable a_local_variable
is local to the local_variable
function in which it is defined. It can only be accessed and used within that function. When the function local_variable
exits, the life of a_local_variable
ends, and its memory is released.
Example
In the following example, the a_local_variable
of integer data type is defined inside the function local_variable()
:
#include <iostream>using namespace std;void local_variable(){int a_local_variable = 1;cout <<a_local_variable;}int main(){local_variable();}
The output of the above code will be:
1
Codebyte Example
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours