Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Variable Scopes | Introduction
C++ Functions

bookVariable Scopes

Variable scope 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
Note

A code block is a set of statements enclosed within curly braces { }.

main.cpp

main.cpp

copy
1234567891011121314
#include <iostream> int example() { // Create variable inside the function int local = 10; return local; } int main() { // Try to access the variable created inside function std::cout << local; }

The error error: ‘localVar’ was not declared in this scope appears because localVar exists only inside MyFunction() and is destroyed after it ends. To access its value, return it from the function and store it in a variable inside main().

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int example() { // Create variable inside the function int local = 10; return local; } int main() { // Assign the result of the function to a new variable int result = example(); std::cout << result << std::endl; }
question mark

What is the lifetime of a local variable?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain the difference between local and global variables?

How do I return a value from a function and use it in another part of my code?

Can you give an example of a scope-related error and how to fix it?

Awesome!

Completion rate improved to 5

bookVariable Scopes

Свайпніть щоб показати меню

Variable scope 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
Note

A code block is a set of statements enclosed within curly braces { }.

main.cpp

main.cpp

copy
1234567891011121314
#include <iostream> int example() { // Create variable inside the function int local = 10; return local; } int main() { // Try to access the variable created inside function std::cout << local; }

The error error: ‘localVar’ was not declared in this scope appears because localVar exists only inside MyFunction() and is destroyed after it ends. To access its value, return it from the function and store it in a variable inside main().

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int example() { // Create variable inside the function int local = 10; return local; } int main() { // Assign the result of the function to a new variable int result = example(); std::cout << result << std::endl; }
question mark

What is the lifetime of a local variable?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4
some-alt