In the previous chapter, the examples of custom function calls I provided all belong to value-based function calls. The characteristic of this type of call is that the actual parameter values are passed sequentially to the formal parameters in the called function’s parameter list. The number of actual parameters must match the number of formal parameters, and their types must be the same.This value-based call does not change the values of the variables in the actual parameters. However, we previously discussed two functions: 1. scanf 2. swap.These two functions can change the values of the parameters when called. Why is that? Because the function call of scanf belongs to address-based (pointer) calls, while the swap function call method belongs to references.In C++, function call methods can be divided into value-based calls, address-based calls, and references.1. Value-Based CallsThis calling method passes the value of the actual parameter to the formal parameter, which means a copy of the actual parameter value is stored in the stack of the called function. In the called function, the value of the formal parameter can change, but it does not affect the value of the actual parameter in the calling function.Let’s look at the following code:
When we run the program, we will find that the output values of variables a and b are 1 and 2, respectively, which means that calling the swap function did not change the internal values of a and b.2. Address-Based CallsThis calling method passes the address value of the actual parameter to the formal parameter, where the formal parameter is a pointer. The scanf series of functions we learned earlier uses pointers as parameters, for example:scanf(“%d”, &x);This makes the formal parameter’s pointer point to the address of the actual parameter. Here, instead of copying the actual parameter to the formal parameter, the formal parameter directly controls the actual parameter through its address, providing a way to change the value of the actual parameter variable. In C++, not only can the address of a variable be passed as an actual parameter, but the address of a function can also be passed as an actual parameter. We will discuss function pointers later!We can rewrite the swap function in the form of an address-based call:
#include<bits/stdc++.h>
using namespace std;
void swap(int* x,int* y){
int t;
t=*x;
*x=*y;
*y=t;
}
int main(){
int i=5,j=6;
swap(&i,&j);
cout<<i<<" "<<j<<endl;
return 0;
}
Here, the & symbol in front of the variable indicates the address-of operator (the & has different functions as a unary operator and a binary operator), and * is the inverse operation of taking the address. The address of a variable can be called a pointer, which is an important concept in C language and one of its key features. C++ retains this feature to be compatible with C. If viewers have learned other languages, they will find that the use of pointers does not appear in other languages (such as Java, Python). This is precisely because C language has the concept of pointers and addresses, making it suitable for embedded development. To understand what a pointer is, one must first understand how data is stored in memory. Each byte in the memory area has a number, which is the “address”; it is equivalent to a room number in a hotel. Since the address can be used to find the required variable unit, it can be said that the address points to that variable unit. For example, a room has a door with a room number 2008. This 2008 is the address of the room, or in other words, 2008 points to that room. Therefore, the address is visually referred to as a pointer. If there is a variable specifically used to store the address of another variable (i.e., a pointer), it is called a “pointer variable.” In the function parameter list of the code above, x and y are pointer variables pointing to integer variables. Note:In C language, data is type-specific,and different types of variables occupy different numbers of bytes in memory.Therefore, a pointer pointing to a variable is also type-specific, meaning it points to the address of a variable of a specific type. When a pointer variable points to data that occupies multiple bytes, it points to the byte with the smallest address number.3. References A reference variable in C++ is an alias, meaning it is another name for an existing variable. Once a reference is initialized to a variable, either the reference name or the variable name can be used to refer to the variable. References in C++ can, to some extent, replace pointers in C language and can directly modify actual parameters in a function using the “pass by reference” method. Now let’s modify the program to swap two numbers as follows:
#include<bits/stdc++.h>
using namespace std;
void swap(int& x,int& y){
int t=x;
x=y;
y=t;
}
int main(){
int a=10,b=20;
swap(a,b);
cout<<a<<" "<<b<<endl;
return 0;
}
If viewers have browsed LeetCode, they will find that the member functions on LeetCode frequently use this reference passing method.