Introduction to Functions |
|
Except when creating a class, all the code we have written so far was confined to main(). This may have made the program simpler but this approach get main() "crowded". As an alternative, instead of performing all your assignments in main(), you can divide the code is smaller or relatively smaller sections of code so that each section is meant to perform a specify task. Other sections of the program can then refer to that section for whatever it has to provide. Such a section of code is called a function. To create a function, there are some rules you must follow. The fundamental formula is: ReturnType FunctionName() { } The first rule is that the function must be created outside of any other function. This means that you cannot create a function inside of main(). The ReturnType is the kind of value that the function will produce. Some functions don't produce a (specific) result. In this case, the ReturnType can be substituted with the void keyword. Like a variable, a function must have a name. The name follows the rules we have applied to variables so far. Because a function usually performs an action or a task, its name should reflect a verb. Examples of function names are Show or Display. In some cases, the name will be made of more than one word. You should still make sure that the name reflects an assignment. The name of a function must be followed by parentheses. The parentheses of a function must be followed by an opening curly bracket "{" and a closing curly bracket "}". The section inside of the curly brackets is referred to as the body of the function. In this body, you perform the assignment of the function. For example, a function can be used to simply display a sentence. Here is an example: void Introduction() { Console::WriteLine(L"The Wonderful World of C++/CLI"); } Writing a function like this, with its name and body, is referred to as defining or implementing it. This appears as a simple assignment. In most cases, a function will be more complex than that. In fact, the body of a function can become quite crowded. For this reason, you should spread its body on different lines. In this case, at least the closing curly bracket should stand on its own line and as the last. Where you place the opening curly bracket, whether on the same line as the name of the function or on its own line is a matter of choice. Based on this, here another way to write the above function: using namespace System; void Introduction() { Console::WriteLine(L"The Wonderful World of C++/CLI"); } int main() { return 0; } In the same way, you can define or implement as many functions as you judge necessary.
After creating a function, you can use it either inside of itself or from another function. Using a function is referred to as calling it. To call a function, type its name, followed by parentheses. If the function is called as a statement, then it must be terminated with a semi-colon. Here is an example: using namespace System; void Introduction() { Console::WriteLine(L"The Wonderful World of C++/CLI"); } int main() { Introduction(); return 0; } This would produce: The Wonderful World of C++/CLI Press any key to continue . . . In the above examples, we defined the function before calling it. As an alternative, you can first declare a function and define it somewhere in your program. The main rule to observe in this case it that you must make sure the compiler can find the definition of the function when calling it. You can declare (but not define) a function inside of another function, then define the function outside. Here is an example: using namespace System; int main() { void IdentifyHouse(); Console::WriteLine(); return 0; } void IdentifyHouse() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); Console::WriteLine(L"\nType of Home: {0}", TypeOfHome); } Once the function has been declared, you can call it even if it is defined later in the source file. Here is an example that calls the above function: using namespace System; int main() { void IdentifyHouse(); IdentifyHouse(); Console::WriteLine(); return 0; } void IdentifyHouse() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); Console::WriteLine(L"\nType of Home: {0}", TypeOfHome); } If you declare a function A inside of another B, that declared function A can be accessed only inside of the second function B. If you want other functions to be able to access a function, you can either define it above all functions that would call it, or declare it globally, that is, above and outside of any function. Here is an example: using namespace System; void IdentifyHouse(); int main() { IdentifyHouse(); Console::WriteLine(); return 0; } void IdentifyHouse() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); Console::WriteLine(L"\nType of Home: {0}", TypeOfHome); }
When you call a function B() from function A(), function A()
sends a request and must get to Function B(). Whenever your program
includes a small function, C++ allows you to include such a function where it is
being called. When function B() calls function A(), instead of sending a request
to function A(), the compiler would include a copy of function A() into function
B() where it is being called. Such a function (function A()) is qualified
inline. inline void IdentifyHouse() When defining the function, use the inline keyword in the same way. You can place the inline keyword before or after the return type or void. Here is an example that makes use of an inline function: using namespace System; void inline IdentifyHouse() { __wchar_t TypeOfHome; Console::WriteLine(L"What Type of House Would you Like to Purchase?"); Console::WriteLine(L"S - Single Family"); Console::WriteLine(L"T - Town House"); Console::WriteLine(L"C - Condominium"); Console::Write(L"Your Choice? "); TypeOfHome = __wchar_t::Parse(Console::ReadLine()); Console::WriteLine(L"\nType of Home: {0}", TypeOfHome); } int main() { IdentifyHouse(); Console::WriteLine(); return 0; }
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|