If I Could Call An Argument, Do You Think Const Would Pass Me By?
Argument Passing by Copy, Reference, Pointer
There are many different ways to call arguments into a function in C++. Unless specified, arguments are passed into function in C++ by copying the contents. Because the copy is stored in a new address, changes that are made to the data within the function are not reflected in the original data outside of the function. In the case we want to mutate the data we are calling into a function, we will need to refer to the data with & in the signature of the function. Because a reference simply refers to a variable, a reference can be made to modify the variable's contents through the variable. Calling the reference should be the default way to pass in data that will be changed by the function.
#include <iostream>
void convertFtoC(double & temperature)
{
temperature= (temperature - 32)*(5.0/9.0);
}
int main(void)
{
double temperature = 212.0;
std::cout<<"Original temperature is " <<temperature<<"F."<<std::endl;
convertFtoC(temperature);
std::cout<<"New temperature is " <<temperature<<"C."<<std::endl;
return 0;
}
It is also possible to passing data by point. By passing the data with * in the signature, the address of the variable will be passed into the function. Passing by pointer is sometimes used to pass around arrays allocated with new, after which elements are accessed with *. However, this is awkward for other uses, where passing by references are preferred.
Const referencing
A const reference can be passed into a function to make an immutable variable. Once a const variable is passed into a function, it cannot be changed within the function. However it is important to maintain const correctness when use const references. A const reference can accept both const and non-const data, but non-const reference can only take non-const objects. Therefore, a non-const reference cannot be made of a const object, you can't try to refer and change a const object. It's generally recommending to make reference arguments const if you do not have any intention of modifying the data, and is importing for maintaining the integrity of large data structures, like vectors and strings. It is much safer to write functions that do not mutate their input parameters, and to avoid global parameters.
#include <iostream>
double convertFtoC(double const temperature)
{
return (temperature - 32)*(5.0/9.0);
}
int main(void)
{
double temperatureF = 212.0;
double tempuratureC=convertFtoC(temperatureF);
std::cout<<"Original temperature is " <<temperatureF<<"F."<<std::endl;
std::cout<<"New temperature is " <<temperatureC<<"C."<<std::endl;
return 0;
}
Function Overloading
In C++, we can have multiple functions of the same name, so long as the functions have different signatures. This is called function overloading Because the compiler will determine which function is used based on the data types and numbers of the argument. It is important to note that functions cannot be overloaded by return type, meaning you cannot have two functions that input the same data type, but return two different types of data. For examples, we can have two convertFtoC functions, where one takes a double and the other takes a std::vector<double>
#include <iostream>
#include <vector>
void convertFtoC(double & temperature)
{
temperature= (temperature - 32)*(5.0/9.0);
}
void convertFtoC(std::vector<double> & temperatures)
{
for (auto & t : temperatures)
{
convertFtoC(t);
}
}
Exceptions
C++ gives us several tools to test for errors. Because C++ requires the code to be free of errors upon compilation, any errors in the code will force the program to terminate. Therefore it is useful to use exceptions to catch areas of code that may be prone to errors to facilitate identifying sources of bug. The try statement allows us to test a section of code for errors. If an error is encountered in the try block, a catch statement can then be used to define a section of code to be executed. At any point a error is caught, a throw statement allows us to make custom errors before the program is aborted.
#include <iostream>
#include <stdexcept>
double convertFtoC(double temperature)
{
double abs_zero = -459.67;
if (temperature <abs_zero)
{
throw std::runtime_error("Temperature is less than absolute zero");
}
return (temperature - 32)*(5.0/9.0);
}
int main(void)
{
try
{
std::cout<<convertFtoC(-500.00)<<std::endl;
}
catch (const std::runtime_error & ex)
{
std::cout<<"Error occurred!"<<std::endl;
std::cout<<ex.what()<<std::endl;
return 1;
}
return 0;
}
std::pair
A pair is a class of std::tuple of two elements. It allows us to store two heterogeneous data types in one unit. The first element can be called by first, and the second can be called by second. a std::pair makes it possible for us to return a unit of two different data types from a function. the keyword auto can be used to have C++ automatically determine the data type called, to the ease of the programmer. However it prudent to not use auto recklessly where data types are not explicitly understood, and where they may be incompatible.
#include <iostream>
#include <utility>
std::pair<std::string, double> convertFtoC(double temperature)
{
double temperature = (temperature - 32)*(5.0/9.0);
std::pair<std::string, double> r {"Fahrenheit", temperature};
return r;
}
int main(void)
{
auto temp = convertFtoC(212.0)
std::cout<<"In "<<temp.first<<", temperature is "<<temp.second<<std::endl;
return 0;
}