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

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.

主站蜘蛛池模板: 墨竹工卡县| 饶阳县| 天镇县| 舞阳县| 福安市| 新郑市| 永春县| 五大连池市| 永安市| 扬中市| 普洱| 青龙| 天柱县| 海丰县| 灵武市| 南平市| 凯里市| 平阳县| 商城县| 思茅市| 盱眙县| 高陵县| 迭部县| 任丘市| 淮南市| 佛冈县| 关岭| 贡嘎县| 贡山| 特克斯县| 甘谷县| 瑞丽市| 鲁甸县| 湘乡市| 黄浦区| 隆尧县| 桐乡市| 娱乐| 天镇县| 曲麻莱县| 溆浦县|