Lesson 11: Learning to work with function parameters in #C

This is lesson 11 in our series, and today we’ll learn how to work with function parameters in #C.

We covered all you need to know about working with functions in C #, including how to call and use them, in the last session. We also covered how to determine and deliver function parameters to some extent. However, in this session, we will focus on the parameters’ capabilities and functions.

The ref and out modifiers will be our initial focus. Like most other programming languages, the C # language distinguishes between send variables with a value of “value by” and submission variables with a reference “by.”

Sending by value, or “value by,” is the default mode in C #. This means that when you pass a variable as a parameter to a function, you’re actually providing a copy of the object rather than a reference to the variable’s actual action address.

But at the other side, this implies that you can modify and operate on the sent input variables as needed. Whatever you perform on the parameter inside the function is executed on the copy, not the original variable, because you are not modifying the main object of that variable in memory.

You can edit the above function by using the ref and out keywords to pass the original version of a variable to the function instead of its value.

: Ref modifier
Read the following example code:

static void Main(string[] args)
{
int number = 20;
AddFive(number);
Console.WriteLine(number);
Console.ReadKey();
}
static void AddFive(int number)
{
number = number + 5;
}

We construct a numeric variable named integer number and give it the value of 21 in the example code above.

The variable is then sent to the AddFive () method, which adds a value of 5 units to it.

Does the real variable number, on the other hand, change?

31 The value of 5 has been added to the copy of the variable number and has only changed within the stated function, hence the answer is no.

We supplied a copy of the number variable rather than a reference to the original object. This is the default mode in C #, and it’s what we’re talking about in most circumstances.

However, if you wish to modify the value of the main object of the variable number, you must use the ref keyword in the argument, as seen below:

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;
}

We utilise the ref keyword not only when defining a function, but also when invoking it, as you can see in the code above.

When you run the programme, you’ll notice that the original value of the variable number, not the copy, has changed.

Tutorial on Using the Modifier:

The modifier out functions similarly to ref. They both create the original version as well as a reference.

Send the variable, not its value, as an argument. However, there are two significant differences:

1) Before calling the modifier ref, the variable that is given to it must have been initialized.

Out, on the other hand, does not have this restriction, and you can send an unnamed variable to it.

2) But at the other side, without assigning a value to an out variable, you cannot call your desired function to the out argument.

True, you can get around this—but only if you’re a technical who knows what he’s doing.

Params Modifier Tutorial:


When defining or calling the functions we’ve defined so far, we’ve received a particular number of parameters.

However, you could want your function to receive and deliver a different number of parameters each time it is called. This is accomplished by passing a variable to the function, such as an array, as shown in the following method:

static void GreetPersons (string [] names) { Of course, calling these types of functions is a bit more complicated than usual, as follows:

GreetPersons (new string [] John “John”, “Jane”, “Tarzan”});

The above method is perfectly correct, but it can be better written as follows. Using the keyword Params:

Static void GreetPersons (params string [] names) {
The above call code is the same as the following call:

38 GreetPersons (“John”, “Jane”, “Tarzan”);

Another advantage of using the Params keyword is that you can also send empty parameters (Parameters, 2re) to the function.

When using Params, the function can receive other types of parameters as well, so the Params parameters must be defined at the end.

On the other hand only one parameter Params in the definition and call of each
The function can be used.

The following example code is a complete example of how to use Params in C #:

static void Main(string[] args)
{
GreetPersons(0);
GreetPersons(25, “John”, “Jane”, “Tarzan”);
Console.ReadKey();
}
static void GreetPersons(int someUnusedParameter, params string[] names)
{
foreach (string name in names)
Console.WriteLine(“Hello, ” + name);
}

THANKS FOR READ THIS COMPLETE ARTICLE ABOUT

Lesson 11: Learning to work with function parameters in #C

%d bloggers like this: