Usage
All C++ applications must have at least one function; this is called main()
. Additionally, you can have user defined functions that encapsulate individual tasks, thus allowing the code to be cleaner and easier to read. Therefore, this is a useful feature if you repeat the same task many time 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;
}
It is easy to spot the obvious repetition here. We can simplify this code by using a function that does all the common work. Anything that is unique we can pass as parameters 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 void
return type then you don't need to Return Statement. With any other return type your function must exit by using a Return Statement. The Return Statement determines the value returned to the caller from the function. The one exception to this rule (and their has to be an exception to make it a rule) is int main()
. If you don't explicitly have a Return Statement int int main()
the compiler will plant return 0;
for you.
Forward Declaration
One thing to note about a function is that you can not use it before a declaration. We rewrite the original example above as:
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 here is that I have moved the main()
function before the getNameFor()
function. This will generate a compilation error as you are using the function getNameFor()
before a declaration. This may seem a potential problem but it is a deliberate technique that makes sure 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 clear 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;
}