当前位置:网站首页>Formal parameters, local variables and local static variables
Formal parameters, local variables and local static variables
2022-04-23 05:27:00 【What’smean】
Formal parameters and variables defined inside the function body are collectively referred to as local variables , They are local to functions , Visible only in the scope of a function . The local variables in the function body are divided into ordinary local variables and static local variables , For formal parameters and ordinary local variables , This object is created when the control path of a function passes through a variable definition statement , Destroy the definition when it reaches the end of the block . We call objects that exist only during block execution automatic objects . Of these concepts difference yes :
- A formal parameter is an automatic object , The function starts by applying memory for a parameter , We initialize the automatic object corresponding to the formal parameter with the arguments provided when calling the function .
- Automatic objects corresponding to common variables are also easy to understand , We create an automatic object at the statement that defines the variable , If the definition statement provides an initial value , Then initialize with this value ; otherwise , Perform default initialization . When the block where the variable is located ends , Variable failure .
- Local static variables are special , Its life cycle runs through function calls and beyond . The object corresponding to a local static variable is called a local static object , Its life cycle starts at the definition statement , Not until the end of the program .
Code help :
#include<iostream>
using namespace std;
double myAdd(double val1,double val2){
double resul = val1+val2;
static unsigned iCnt = 0;// Static local variables ( Throughout the process )
++iCnt;
cout<<" This function performs a total of "<<iCnt<<" Time "<<endl;
return resul;
}
int main(){
double num1,num2; // Ordinary local variables
cout<<" Please enter two numbers :"<<endl;
while(cin>>num1>>num2){
cout<<" The sum result is :"<<myAdd(num1,num2)<<endl;
}
return 0;
}
Output results :
Please enter two numbers :
2.4
3.4
This function performs a total of 1 Time
The sum result is :5.8
33
33
This function performs a total of 2 Time
The sum result is :66
43
34
This function performs a total of 3 Time
The sum result is :77
^Z
Please press any key to continue . . .
版权声明
本文为[What’smean]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220544124210.html
边栏推荐
猜你喜欢

弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化

Uncle wolf is looking for a translator -- Plato -- ongoing translation

The 2021 IT industry project management survey report was released!

varnish入门

What are the most popular recruitment technical skills in 2022? You can't think of it

Cloud computing and cloud native architecture design of openshift

Cross domain CORS relationship~
![[triangle Yang Hui triangle printing odd even cycle JS for break cycle]](/img/9a/6cdc00e6056a1a47d2fbb8b9a8e975.png)
[triangle Yang Hui triangle printing odd even cycle JS for break cycle]

領域驅動模型DDD(三)——使用Saga管理事務

Kanban Quick Start Guide
随机推荐
Edit, cancel, pull up menu
How to add beautiful code blocks in word | a very complete method to sort out and compare
what is wifi6?
Membarrier (personal learning and understanding)
Self incrementing sequence creation of MySQL
Laravel database
跨域CORS的情缘~
Domain driven model DDD (III) -- using saga to manage transactions
What are the reasons for the failure of digital transformation?
Tslint annotations ignore errors and restful understanding
Solve the problem of JS calculation accuracy
Uniapp hot update with progress bar
[untitled]
CPT 104_TTL 09
Parsing of string class intern() method
Nécessité de précharger les cookies dans le sélénium
!!!!!!!!!!!!!!!!!!
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
Uniapp wechat sharing
Understand the relationship between promise async await