C++ value passing in and out detailed explanation

We know that most of the functions we define usually take parameters to pass in and out data. Below, I will give an example to demonstrate the incoming and outgoing process.

1. No incoming, no outgoing .

class Cout{
public:
	static int x;
	static int y;
	int add();
};
int Cout::x=5;
int Cout::y=6;
int Cout::add()
{
	int z=x+y;
	return z;
}
int main()
{
	Cout c;
	cout<<c.add()<<endl;
}

Result:
Insert picture description here
Analysis : These are two static member variables in the calculation class. We did not pass parameters in the add function, and the results can also be obtained.

2. There is incoming, no outgoing .

int Add(int x,int y)
{
	return x+y;
}
int main()
{
	cout<<Add(1,2)<<endl;
}

Result:
Insert picture description here
Analysis : This is a simple addition function. It only passes parameters to the function, not parameters, but we can get the desired result through return.

3. No incoming, but outgoing .

class Send{
public:
	bool A(char* a);
	bool B(char* b);
};
bool Send::B(char* c)
{
	char b[4]="aaa";
	strcpy(c,b);
	return true;
}

bool Send::A(char* a)
{
	if(this->B(a))
		return true;
	return true;
}

int main()
{
	Send send;
	char k[5];
	send.A(k);
	cout<<k<<endl;
}

Results:
Insert picture description here
Analysis : Send class which defines the two functions A and B, B's role is to copy the string "aaa" to address the address c , the role A is calling B function .
When execute send.A(k), enter the A function, at this time a is k, call the B function B(a)=B(k), enter the B function, at this time, c is k, strcpy(c,b) Is strcpy(k,b), at this time k is "aaa", and the A function can get the value of k at this time.

4. There is incoming and outgoing .

class Send{
public:
	bool A(char*b,char* c);
	bool B(char* c,char* d);
};
bool Send::B(char* c,char* d)
{
	strcpy(c,d);
	return true;
}
bool Send::A(char* a,char* c)
{
	if(this->B(a,c))
		return true;
	return true;
}
int main()
{
	Send send;
	char k[5];
	send.A(k,"AAA");
	cout<<k<<endl;
	return 0;
}

result:
Insert picture description here

Analysis : The above is an extension of the third case. The Send class is defined. There are two functions, A() and B(). The function of the B function is to copy the string in the address d to the address c, and the function A The function is to pass in the address of the string and pass it into the B function.

May be a bit difficult to understand

Step by Step is:
When we send.A (k, "AAA") , first enter the function A
at this time A function of c became "AAA", and then call the B function B (k, "AAA")
then Enter the B function. At this time, the c and d in the B function are k and "AAA"
and then strcpy(c,d); is strcpy(k,"AAA"), at this time, k="AAA".
After we call, we can get the value of k.

After reading this article, do you have a deeper understanding of the incoming and outgoing values?

Insert picture description here

Guess you like

Origin blog.csdn.net/baidu_41191295/article/details/112549399