官术网_书友最值得收藏!

Scope of variables

You may have noticed that we have two variables called c in the program right now. There is a c in the main function as well as a c in the add function. How is it that they are both named c but have different values?

In C++, there is the concept of a local variable. This means that the definition of a variable is confined to the local block of code it is defined in. Consequently, the c variable in the add function is treated differently to the c variable  in the main function.

There are also global variables, which need to be declared outside of the function or block of code. Any piece of code written between curly brackets is considered to be a block of code. Consequently, for a variable to be considered a global variable, it needs to be in the body of the program or it needs to be declared outside a block of code of a function.

主站蜘蛛池模板: 进贤县| 乌拉特前旗| 溧阳市| 牙克石市| 潼关县| 西峡县| 固原市| 石楼县| 蓬溪县| 公主岭市| 大足县| 黄浦区| 江津市| 西吉县| 循化| 思茅市| 肥东县| 囊谦县| 泽库县| 武陟县| 灌阳县| 饶平县| 柳江县| 山东省| 安丘市| 内江市| 长沙县| 兰考县| 大新县| 民权县| 哈尔滨市| 岢岚县| 无极县| 韩城市| 曲周县| 邻水| 德安县| 奎屯市| 广安市| 扶余县| 木里|