Usage
All C++ applications must have at least one function called main()
. Additionally, you can have user-defined functions that encapsulate individual tasks, thus making the code cleaner and easier to read. Therefore, this is a useful feature if you repeat the same task many times with only slight variations:
function1.cpp
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "What is your first name?\n";
std::string firstName;
std::cin >> firstName;
std::cout << "What is your second name?\n";
std::string secondName;
std::cin >> secondName;
std::cout << "What is your Mother's name?\n";
std::string motherName;
std::cin >> motherName;
std::cout << "What is your Father's name?\n";
std::string fatherName;
std::cin >> fatherName;
}
The obvious repetition here is easy to spot. We can simplify this code using a function that does all the common work. We can pass anything unique as a parameter to the function.
function2.cpp
#include <string>
#include <iostream>
std::string getNameFor(std::string who)
{
std::cout << "What is your " << who << " name?\n";
std::string result;
std::cin >> result;
return result;
}
int main(int argc, char* argv[])
{
std::string firstName = getNameFor("first");
std::string secondName = getNameFor("second");
std::string motherName = getNameFor("Mother's");
std::string fatherName = getNameFor("Father's");
}
Definition
OK. We have seen an example, but what is the exact format of a function?
function3.cpp
<ReturnType> <FunctionName>(<OptionalArgumentList>)
{
<OptionalCode>
}
If a function has a void
return type, you don't need to Return Statement. With any other return type, your function must exit using a Return Statement. The Return Statement determines the value returned to the caller from the function. The one exception to this rule (and there must be an exception to make it a rule) is int main()
. If you don't explicitly have a Return Statement in int main()
, the compiler will plant return 0;
for you.
Forward Declaration
One thing to note about a function is that you cannot use it before a declaration. We rewrite the original example above as follows:
function4.cpp
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
std::string firstName = getNameFor("first");
std::string secondName = getNameFor("second");
std::string motherName = getNameFor("Mother's");
std::string fatherName = getNameFor("Father's");
}
std::string getNameFor(std::string who)
{
std::cout << "What is your " << who << " name?\n";
std::string result;
std::cin >> result;
return result;
}
The only difference from above is that I have moved the main()
function before the getNameFor()
function. This will generate a compilation error as you use the getNameFor()
function before a declaration. This may seem like a potential problem, but it is a deliberate technique that ensures you spell things correctly before use. In the above situation, the only change you need to make is a forward declaration. This allows you to declare a function before you define it. The utility of this will become apparent when we start defining modules.
function5.cpp
#include <string>
#include <iostream>
extern std::string getNameFor(std::string who);
int main(int argc, char* argv[])
{
std::string firstName = getNameFor("first");
std::string secondName = getNameFor("second");
std::string motherName = getNameFor("Mother's");
std::string fatherName = getNameFor("Father's");
}
std::string getNameFor(std::string who)
{
std::cout << "What is your " << who << " name?\n";
std::string result;
std::cin >> result;
return result;
}