Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
8.1 Mб
Скачать

9. Introduction to Functions

9.1 Functions

9.1.1 Definition

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.

9.1.2Fundamentals of Creating 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.

9.1.3Calling a Function

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;