Sunday, June 23, 2024 20:34

Table of contents >> Functions > Methods and functions parameters

Methods and functions parameters

Our methods and functions can perform different actions, but sometimes, we need to offer them certain relevant data to process. We do this by using methods and functions parameters.

One of the primary goals of methods and functions is what we call code re-usability and this means we don’t have to copy and paste the same piece of code over and over, whenever we need that certain action to be performed.

Let’s consider for example that we want to calculate the area of a few rectangles. You already know that the area of a rectangle is the width of the rectangle, multiplied by its height. We can do it like this:

The above code works like a charm, and produces the following output:

rectangle area output

However… as a general rule, here is a nice programming tip: whenever you have to re-write something that looks awfully similar to a previous piece of code, YOU ARE DOING SOMETHING WRONG! Copy-pasting code is a major “no-no!” in programming!

So, what is the correct solution? Well, wouldn’t it be really, really great to have a single piece of code that would give us the area of ANY rectangle we want? We can achieve this by using a method. And because we want our method to process the area of any rectangle, we also need to “tell” that method the values of the widths and heights of our various rectangles. How do we do that? Through parameters, like this:

You notice two first things: the output of the above code is exactly the same as in the first example. Second, the above code is much shorter and cleaner than our first example.

Now, lets dissect what we have done in our second example. First, inside our class, we have created a method called CalculateRectangleArea() (we know it is a method, not a function, because it has the void keyword in front of its declaration name; don’t bother with the static keyword for now, we will explain it later). We have done this before, nothing new so far. Still, looking at our example, we notice a small difference: our method has some text inside its declaration parenthesis, unlike our previous lessons examples. What does float rectangleWidth, float rectangleHeight mean? As you might have guessed, these are the parameters of our method. When we call this method, since we have two parameters of type float, we must provide the call with two float values. If we don’t, we will receive an error:

Because in our Main() method we called CalculateRectangleArea() without specifying two float values between the parenthesis, and because we declared our CalculateRectangleArea() method as having two mandatory float parameters, Visual Studio will display the following error:

no parameters error

So, basically, when we declare a method or a function, we can specify parameters to them by declaring those parameters inside the parenthesis of our method or function declaration. The way we specify a parameter is like any usual variable declaration: we must write the type of the parameter (float, in our example) and its name (rectangleWidth, rectangleHeight, etc). We can specify as many parameters as we want, and we must separate them with coma. To sum it up, the blueprint for specifying methods and functions parameters is this:

where <parameters_list> is a list of zero or more declarations of variables, separated by a coma:

Parameters of methods and functions can be of any type: primitive types (int, string, double, float, etc) or object types (arrays, lists, classes, etc).

Once you declare one or more parameters to a method or a function, the parameter variable(s) are available inside the body of that method or function. In our case, rectangleWidth and rectangleHeight can be used inside the block of our method. Whenever we call CalculateRectangleArea() and we specify two float values, those two values are assigned to our float parameters, which in turn can be used inside the method. Please note that methods and functions parameters are only visible inside the body of their declaring method or function! We cannot use them or their values anywhere else in our program, directly!

Lastly, functions parameters are identical with methods parameters:

Of course, the difference is that our functions must return a value (of type float, as our function declaration specifies), which we might just as well use directly when we call the function.

If some of you have already figured out that Console.WriteLine() is also a method to which we pass a parameter of type string, bravo! 😉

Whenever we use

we are actually calling a method called WriteLine(), inside the Console class, to which we pass a string value (“Hello world!”), because its declaration specifies that it expects a parameter of type string.

Another useful tip is that if we right click on the name of any function or method, we have a few useful options:

functions and methods context menu visual studio

  • Peek Definition will open a small window where we can see the declaration of that method or function. Useful for quickly seeing the declaration model of that method or function
  • Go To Definition will make Visual Studio to immediately jump to the declaration of that method and function. Useful if we want to study in detail the declaration and the body content of a method or function.
  • Go To Implementation is of no importance to you right now, we will deal with it when we will learn Interfaces.
  • Find All References will display in our output window a list of all the places where that method or function is being called. Very useful if we have a large project, where a method or function is called in many parts of the code. Double clicking one of the entries in that list will make Visual Studio jump to that particular line.

The concepts explained in this lesson are also shown visually as part of the following video:

Tags: , , ,

Leave a Reply

Follow the white rabbit