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

Void SetGender(__wchar_t a);

double RectangleArea(double L, double W);

char Admission(char Category, double Grade, char g);

The type of value that a function needs is called a parameter. The actual value that would be processed by the function is called an argument. Supplying the value to a function is referred to as passing the argument. When a function is declared, the argument must be specified by its data type. When the function is called, the argument can be passed using the name of a variable of the same type. You can also pass an actual value to the function instead of a variable name.

When a function receives an argument, it can ignore that argument. Here is an example:

void Multiply(double Value)

{

Console::WriteLine(L"Multiplication");

}

To call a function that takes an argument, specify the name of the function and its list of arguments (if any) inside of parentheses. You can pass the constant value of an argument. Here is an example:

using namespace System;

void Multiply(double Value)

{

Console::WriteLine(L"Multiplication");

}

int main()

{

Multiply(25.75);

return 0;

}

This would produce:

Multiplication

When calling the function, if the value is stored in a variable, you can pass the name of the variable in place of the argument. Here is an example:

using namespace System;

void Multiply(double Value)

{

Console::WriteLine(L"Multiplication");

}

int main()

{

double HourlySalary = 25.75;

Multiply(HourlySalary);

return 0;

}

When defining a function that takes a parameter, in the body of the function, you can use the argument appropriately as you see fit. One the minimum operations you can perform is to involve the argument in an expression. Here is an example:

using namespace System;

void Multiply(double Value)

{

double Weekly;

Weekly = Value * 36.50;

Console::WriteLine(L"Hourly Salary: ${0}", Value);

Console::WriteLine(L"Weekly Salary: ${0}", Weekly);

}

int main()

{

double HourlySalary = 25.75;

Multiply(HourlySalary);

return 0;

}

This would produce:

Hourly Salary: $25.75

Weekly Salary: $939.875

You can also define a function that returns the value of the argument. Here is an example:

double Multiply(double Value)

{

return Value;

}

Remember that, depending on the goals you set for your function, you can use as many parameters as you want. Here is example:

Void CalculateRateAmount(double price, double rate)

{

}

As seen in previous sections, the return keyword is used to return an appropriate value from a non-void function. In fact, the item on the right side of the return keyword could be a constant value or a complete expression. The compiler is able to evaluate this item and find out whether it is conform to the return type on the left side of the function name. For this reason, inside of the function that performs a calculation and returns its result, you don't have to first declare a variable that would hold the return value; as long as you provide an appropriate value or expression on the right side of the return keyword, the compiler would be satisfied. Here is example:

double CalculateRateAmount(double price, double rate)

{

return price * rate / 100;

}

To call a function that takes more than one parameter, provide a value for each parameter in the parentheses of the function, in the order the parameters are listed in the function.

When declaring a function, the compiler doesn't require that you supply a name for each argument, it only needs to know the type of argument(s) and the number of arguments a function takes. This information is provided by the presence of a data type. This means you can declare a function with only the data type of an argument. Here is an example:

double CalculateRateAmount(double, double);

When defining such a function that has been declared as done above, you must provide a name for each argument that you intend to use.

The compiler also doesn't care about the name you give an argument when declaring a function. When implementing the function, you just have to remember to use the same name(s) included in the parentheses of the function.