Functions
A function can return a value after it finishes. The type of the return value must be specified at the beginning of the function and the return statement on the function should have a value of the matching type. For example, these two functions have the same name but a different return type and parameter type:
int divide(int a, int b) {
return a / b;
}
float divide(float a, float b) {
return a / b;
}
So those functions have different return values, for example in this situation:
int main()
{
std::cout << divide(5, 2) << std::endl;
// Adding .0f makes them floats
std::cout << divide(5.0f, 2.0f) << std::endl;
}
The first divide will print out 2 since it is an integer division where the remainder is discarded. The second one will print out 2.5 since it is a float division.
Your task is to create two functions called add
, one which adds integers together, and another one which combines two strings together.
#include <iostream>
#include <string>
int main()
{
std::cout << add(4, 5) << std::endl;
std::cout << add("4", "5") << std::endl;
}
9
45