Methodological Instructions
Theme: User functions and procedures
Aim: write code in programming language using functions and procedures
Assessment criteria
Knowledge
· Know what is function
· Know what is procedure
Understanding
· Describes the difference between function and procedure
Application
· Write code using functions
· Write code using procedures
Analysis
· Analyze function code
Key words and phrases:
Void, IntelliSense, procedure, function, bracket, return value
Useful expressions for dialogs and letters:
Well, there are cases when we use returning … .
For example, the IntelliSense is of considerable support when … .
Obviously, the functions and procedures are used to make … .
Theory
The first thing that we will take a look at, is the out and ref modifiers. C#, and other languages as well, differ between two parameters: "by value" and "by reference". The default in C# is "by value", which basically means that when you pass on a variable to a function call, you are actually sending a copy of the object, instead of a reference to it. This also means that you can make changes to the parameter from inside the function, without affecting the original object you passed as a parameter.
With the ref and the out keyword, we can change this behavior, so we pass along a reference to the object instead of its value.
Consider the following example:
static void Main(string[] args)
{
int number = 20;
AddFive(number);
Console.WriteLine(number);
Console.ReadKey();
}
static void AddFive(int number)
{
number = number + 5;
}
We create an integer, assign the number 20 to it, and then we use the AddFive() method, which should add 5 to the number. But does it? No. The value we assign to number inside the function, is never carried out of the function, because we have passed a copy of the number value instead of a reference to it. This is simply how C# works, and in a lot of cases, it's the preferred result. However, in this case, we actually wish to modify the number inside our function. Enter the ref keyword:
static void Main(string[] args)
{
int number = 20;
AddFive(ref number);
Console.WriteLine(number);
Console.ReadKey();
}
static void AddFive(ref int number)
{
number = number + 5;
}
As you can see, all we've done is added the ref keyword to the function declaration as well as to the call function. If you run the program now, you will see that the value of number has now changed, once we return from the function call.
П. Questions for self-assessment.
1) What is the difference between procedure and function?
2) How to call a procedure or function?
3) How to use procedure or function?
Visual Aids and Materials links.
1. Slides
2. https://csharp.net-tutorials.com/basics/functions/
3. https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/keywords/value-types
4. https://poisk-ru.ru/s30898t9.html
5. https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/
Students' Practical Activities:
Students must be able:
· Write code using functions
· Write code using procedures
Скачано с www.znanio.ru
Материалы на данной страницы взяты из открытых источников либо размещены пользователем в соответствии с договором-офертой сайта. Вы можете сообщить о нарушении.