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

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.

主站蜘蛛池模板: 屯昌县| 商城县| 天峻县| 临海市| 西峡县| 长丰县| 新巴尔虎右旗| 英德市| 凤庆县| 洪雅县| 绥宁县| 开封市| 抚顺市| 麻阳| 理塘县| 无锡市| 平泉县| 沈丘县| 鄂托克前旗| 温泉县| 和政县| 汉寿县| 嘉义市| 许昌市| 临泽县| 象山县| 古丈县| 新绛县| 恩施市| 昭通市| 商都县| 淮南市| 海林市| 慈溪市| 芜湖市| 东源县| 从化市| 太湖县| 沂南县| 京山县| 布拖县|