Do function parameter variables always require a & or * operator?

Those * and & in your code are neither dereference nor address-of operators. Both, * and & can have different meanings. Here they are part of the type:

int x;      // declares an int
int* p;     // declares a pointer to int
int& r = x; // declares a reference to int

The address-of and dereference operators come into play for example when you assign something to the above variables:

p = &x; // here & is address-of operator
x = *p; // here * is dereference operator
int someFunction(std::vector<int>& nums) {
     //do stuff 

In this context the & is being used to declare that the address of the nums variable is being used rather than the value.

No. Here the & means that nums is passed as reference.

If this signature is changed to the below, the value is being used

int someFunction(std::vector<int>* nums) {
     //do stuff 

No. Here nums is passed as pointer.

Only this is pass-by-value:

int someFunction(std::vector<int> nums) {
     //do stuff 

Browse More Popular Posts

Leave a Comment