Constant Function Arguments
In C++, constant arguments in a function indicate that the values passed to the function as parameters cannot be modified inside the function. To declare the constant argument, we have to use const
keyword before the type specifier of the argument inside the signature of the function:
Now let's consider the difference between using const
with pass by value and pass by pointer/reference arguments.
Pass const arguments by value
When a parameter is passed by value and declared as const
, it means a copy of the value is made, and the function cannot modify that copy.
In this case the const
qualifier serves as documentation, indicating to other developers that the function does not modify the parameter:
example.h
The const
qualifier ensures that the number
parameter cannot be modified within the calculateSquare()
function, and we can be sure about the integrity of the copied data.
Pass const arguments by pointer/reference
Using constants with pointers or references ensures the preservation of the original data, unlike when arguments are passed by value. While memory optimization often necessitates passing parameters through pointers or references, it becomes crucial to maintain the integrity of the original data within the function.
main.cpp
Code Description
-
const double* radiusPtr
: A pointer to a constant double representing the radius of the circle. The pointer is marked as const, indicating that the function does not modify the value the pointer points to.-
const double& pi
: A reference to a constant double representing the value of pi. The reference is marked as const, indicating that the function cannot modify the value of pi.calculateArea()
function, it checks if the radius (pointed to by radiusPtr
pointer) is greater than 0. If it is, the function calculates the area of the circle area and returns the result. If the radius value is not valid (less than or equal to 0), the function returns 0.
¿Todo estuvo claro?
Contenido del Curso
C++ Functions
2. Function Arguments Specification
3. Function Return Values Specification
4. Some Advanced Topics
C++ Functions
Constant Function Arguments
In C++, constant arguments in a function indicate that the values passed to the function as parameters cannot be modified inside the function. To declare the constant argument, we have to use const
keyword before the type specifier of the argument inside the signature of the function:
Now let's consider the difference between using const
with pass by value and pass by pointer/reference arguments.
Pass const arguments by value
When a parameter is passed by value and declared as const
, it means a copy of the value is made, and the function cannot modify that copy.
In this case the const
qualifier serves as documentation, indicating to other developers that the function does not modify the parameter:
example.h
The const
qualifier ensures that the number
parameter cannot be modified within the calculateSquare()
function, and we can be sure about the integrity of the copied data.
Pass const arguments by pointer/reference
Using constants with pointers or references ensures the preservation of the original data, unlike when arguments are passed by value. While memory optimization often necessitates passing parameters through pointers or references, it becomes crucial to maintain the integrity of the original data within the function.
main.cpp
Code Description
-
const double* radiusPtr
: A pointer to a constant double representing the radius of the circle. The pointer is marked as const, indicating that the function does not modify the value the pointer points to.-
const double& pi
: A reference to a constant double representing the value of pi. The reference is marked as const, indicating that the function cannot modify the value of pi.calculateArea()
function, it checks if the radius (pointed to by radiusPtr
pointer) is greater than 0. If it is, the function calculates the area of the circle area and returns the result. If the radius value is not valid (less than or equal to 0), the function returns 0.
¿Todo estuvo claro?