And a transmission parameter value or an address on a custom C ++ function

Zero directory

  • A preparation
  1. Real participation parameter
  2. Global Variables
  3. Non-global variables (also known as local variables)
  • Two or transport address value
  1. Transfer value
  2. Transport address
  3. Compared
  • Tri-Service General Code

A preparation

- explain the concepts involved in this module will explain.

A real parameter participating

Parameter: that is, "formal parameter" is a parameter defining a function name and the function thereof will be used, the purpose is to receive the parameters passed on the other calls the function code fragment . For example, you define a function int cgz(int n, int m);where n, m is a parameter, which is only an "empty", the received argument values transmitted. Or that the value of the arguments passed to the formal parameter is a ...... fill in the blank?

Argument: that the "actual parameter" is a parameter passed to the function when you call, that is passed to the called function is a value . For example, you define the main function of the two variables a, b, and write the next line of C ++ Standard Library of the max () function, then you wrote max(a, b);, when the a, b is the argument, they put their value is transferred to the max () function processing, and then returns a maximum value.

2 global variables

Some custom function, when defining programmers do not write on the parameter statement. like this:

void example( );
             ^无形参
或:void example(void);
                 ^^^^无形参

Then if the program to be processed in a calculation function, it is common to use a global variable.

A & Q
Q: So what does it mean to be a global variable?
A: global variables, simple point that is not in any custom function (including the main function) affirmed that definition, but defined outside of any function of the variables.

For example this process, a variable is not in any defined functions (including a main function) in the definition stated , this is a global variable.

#include<bits/stdc++.h>
using namespace std;
int a; //在所有自定义函数(包括主函数)之外定义。
int by_myself(void){
	return a - 1; //别误会,这只是用到了a,不是在这个函数中定义的。
}
int main(){
	a = by_myself(); //调用自定义函数。
	return 0;
}
注:程序中有些地方尚未讲解,下面会涉及到。

3 non-global variables (also known as local variables)

Non-global variables (also known as local variables), also known as internal variables. Local variables are as defined in the stated function . Its scope is limited to within its defined function , leave the function before using this variable is illegal.
Look at the code to understand the following:

int example(int a){
	int b,c;
	…… //若干个操作处理语句
}a,b,c作用域

This code is of a parameter, this can be called internal function; b, c local variables.

Second transmission value or address

Numerical transmission 1

When the above parameter to explain the real participation, if notice is the argument own custom value is transferred to the function of the parameter ? This is the calling function argument values to the transmission of a parameter of the called function . (This may be a bit hard to pronounce, you can read several times, we must get to know, is very important)
on actual participation parameter, above, very clear, not repeat them here.

2 transport address

First example:

#include <iostream>
using namespace std;
void Add1(int *a) {
	(*a) ++;
}
void Add2(int a) {
	a ++;
}
int main() {
	int x = 1, y = 5;
	Add1(&x);
	Add2(y);
	cout << x << " " << y << endl;
	return 0;
}
输出:
2 5

Since these two functions added, Addl is passed a pointer, Add2 is the value passed .
(Hereinafter, two very important)

When calling Addl (& x), the system first construction pointer to int a, then the address of argument x passed pointer a, so at this time a and & x is pointing to the same address, i.e., shared unified data, when the inside address a in manipulate data, x is to be operated. When a ++ naturally to x ++, when the function call ends, a freed pointer, the value of x at this time has changed.

It can be said that this method is to put two data are "bundled" in a piece.

When calling Add2 (y), to build a system is a variable of type int, then the value y is transmitted to a (this case is a two variables y and different addresses, but both have the same value), and a ++, y but then no action, so that after the function call, the release of a, y and without any change;

This method is mainly a function of the next internal operation and services, has no effect on the main function.

3 Comparison

This is the difference between the two, parameter passing the address of a pointer type , the numerical transfer function is simply to make the call to "know" a value .

The choice of the kind of transfer, depends on the specific purpose and function of this program, the general parameters to be modified to use the address of the transfer, but only call other data parameters were calculated data itself does not need to modify the values ​​appropriate to pass.

Total code

//cpp
#include<bits/stdc++.h>
using namespace std;
int n = 6; //全局变量n,在所有自定义函数(包括主函数)之外定义。
int by_myself(void){ //无形参
	return n - 1; //别误会,这只是用到了n,不是在这个函数中定义的。
}
void Add1(int *a) { //地址传输
	(*a) ++;
}
void Add2(int a) { // 传输数值
	a ++;
}
int main(){
	int x = 1, y = 5; //定义局部变量 x, y
	n = by_myself(); //调用自定义函数。
	Add1(&x);
	Add2(y);
	cout << n << " " << x << " " << y << endl;
	return 0;
}
输出:
5 2 5
Released four original articles · won praise 6 · views 907

Guess you like

Origin blog.csdn.net/qq_45698847/article/details/105214270