当前位置:网站首页>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
边栏推荐
猜你喜欢
Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
双击.jar包无法运行解决方法
The 2021 IT industry project management survey report was released!
跨域CORS的情缘~
what is wifi6?
C# ,类库
JSP -- Introduction to JSP
Fast application fuzzy search
Three 之 three.js (webgl)旋转属性函数的简单整理,以及基于此实现绕轴旋转的简单案例
Create process memory management copy_ Mm - processes and threads (IX)
随机推荐
After NPM was upgraded, there was a lot of panic
Create process memory management copy_ Mm - processes and threads (IX)
Semi synchronous replication of MariaDB
2021-11-08
When is it appropriate for automated testing? (bottom)
Understand the relationship between promise async await
[untitled]
点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
巴普洛夫与兴趣爱好
Branch and loop statements
(十一)vscode代码格式化配置
Open source rule engine - Ice: dedicated to solving flexible and complex hard coding problems
Getting started with varnish
Output string in reverse order
4 most common automated test challenges and Countermeasures
Laravel routing job
Simple and basic use of switch and if
Laravel database
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
X86 assembly syntax: at & T and Intel