f2
is taking it’s arguments by reference, which is essentially an alias for the arguments you pass. The difference between pointer and reference is that a reference cannot be NULL. With the f
you need to pass the address (using & operator) of the parameters you’re passing to the pointer, where when you pass by reference you just pass the parameters and the alias is created.
Passing by const reference (const double& ref
) is preferred when you are not going to change the arguments inside the function, and when you are going to change them, use non-const reference.
Pointers are mostly used when you need to be able to pass NULL
to your parameters, obviously you’d need to check then inside your function if the pointer was not NULL
before using it.
Related Posts:
- Return array in a function
- C++ pass an array by reference
- C++ correct way to return pointer to array from function
- Is C++ Array passed by reference or by pointer?
- What are the differences between a pointer variable and a reference variable in C++?
- C++ — expected primary-expression before ‘ ‘
- What exactly is the difference between “pass by reference” in C and in C++?
- What does “dereferencing” a pointer mean?
- Regular cast vs. static_cast vs. dynamic_cast
- What is a smart pointer and when should I use one?
- What does “dereferencing” a pointer mean?
- Meaning of = delete after function declaration
- What is a char*?
- What exactly is nullptr?
- How to fix a “invalid operands to binary expression” error?
- What is a dangling pointer?
- What does `*&` in a function declaration mean?
- (->) arrow operator and (.) dot operator , class pointer
- What is uintptr_t data type
- What is the difference between const int*, const int * const, and int const *?
- C++ – No matching member function for call to ‘push_back’
- What is uintptr_t data type
- What does int & mean
- What is the difference between const int*, const int * const, and int const *?
- Pointer to incomplete class type is not allowed
- invalid use of non-static member function
- Reverse Contents in Array
- c++: No instance of overloaded function
- Passing a 2D array to a C++ function
- error: expected primary-expression before ‘)’ token (C)
- Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- How to access the contents of a vector from a pointer to the vector in C++?
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- What does int & mean
- C++: Expression must have a constant value when declaring array inside function
- Sorting Linked List C++ with pointers
- Delete 2D array C++
- error: “initializer expression list treated as compound expression”
- Is the sizeof(some pointer) always equal to four?
- C++ Expression must have pointer-to-object type
- C++ strings and pointers
- Getting error: ISO C++ forbids declaration of with no type
- initial value of reference to non-const must be an lvalue
- Typedef function pointer?
- How to initialize a vector of pointers
- What is the use of intptr_t?
- C++ – Assigning null to a std::string
- Function for C++ struct
- c++ –
- error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- Using C-string gives Warning: “Address of stack memory associated with local variable returned”
- Reference to non-static member function must be called
- Why use pointers?
- What does it mean that “a declaration shadows a parameter”?
- C++ – statement cannot resolve address for overloaded function
- How do I return a char array from a function?
- What’s the difference between * and & in C?
- How to return a struct from a function in C++?
- Why can’t we pass arrays to function by value?
- template argument deduction/substitution failed, when using std::function and std::bind
- Function stoi not declared
- C++ Array of pointers: delete or delete []?
- Why should I use a pointer rather than the object itself?
- ERROR: Control may reach end of non-void function /
- Function stoi not declared
- function does not take 1 arguments c++
- Warning: comparison of distinct pointer types
- Initializing pointers in C++
- Difference between the int * i and int** i
- “cannot be used as a function error”
- set head to NULL (‘NULL’ : undeclared identifier)
- What is a `char*`?
- clearing a vector of pointers [duplicate]
- C++ Swapping Pointers
- Array of Linked Lists C++
- Differences between unique_ptr and shared_ptr
- What is a segmentation fault?
- Split a string using C++11
- error: expected unqualified-id error: Meaning and fix? [duplicate]
- How to track down a “double free or corruption” error
- How to get current time and date in C++?
- where is the official c++ documentation
- wntdll.pdb not loaded – Can’t see the exception
- Networking with C++
- Error a function-definition is not allowed here before ‘{‘ token
- How to fix ‘std::logic_error’ what(): basic_string::_M_construct null not valid error?
- General way of solving Error: Stack around the variable ‘x’ was corrupted
- error MSB3073: How do I fix this?
- timestamp of time(0) at multiple places in a C++ program
- how to initialize an empty integer array in c++
- What does |= mean in c++
- Error: cannot declare variable ‘c’ to be of abstract type ‘circle’
- How does c++ represent negative value
- Use of undefined type [duplicate]
- Warning : overflow in implicit constant conversion
- C++ multiline string literal
- Getting a bunch of crosses initialization error
- c++ a heap has been corrupted error when running the program
- Converting bool to text in C++