Five Minute C# — Lesson 7

Parameters, arguments, and importing a module

Alexander Laurence
Five Minute C#
5 min readOct 28, 2018

--

Lessons: 12345678910 ・ 11 ・ 12 ・ 13 ・ 14

Having an argument with myself

So we know how to make our own methods/functions. Look at you!

So let’s jump right into parameters. Here’s an example of a function making use of a parameter:

Within the parentheses of the Apples() function is the value n. We can therefore call n a parameter of Apples().

But what good does that do? Well, later when we call Apples(), we will be able to input anything we want for a future value for n. Also, the nice thing about parameters is that you can have as many as you like for a function!

Next, arguments are what we call the values of the parameters, which we pass into the function. For example:

The function Apples() was called with the parameter n set to the argument 10. Remember when you call a function, pass the same number of arguments as there are parameters.

You can even declare an object type as parameters.

This is called parameter passing. Why do we need to use this? It can be useful when you want to return a value.

In lesson 6, we talked about functions returning values. In all of our examples, we voided the return type. This means, it will not return a value as it has been void. However, if we specify the return type, we will get a value according to what we told it to give us. For example…

And then if we call the Sum(int a, int b) function and pass the arguments, let’s say 10 for a and 5 for b, and store the output into an integer called answer:

We can now display the answer:

To which, we should get:

Great! That’s it for parameters and arguments (for now).

Now for something different… importing modules!

Remember how the C# compiler reads our script in terms of namespaces, classes, and functions? Sometimes we may need to add ‘plugins’ to our script. We do this by specifying exactly what we want to import at the very top of our script, ahead of everything else. By importing a module, we include a whole new set of definitions (including variables and functions) to play around with. A module is a file and forms part of a library.

For example, we want to include the System module. This will now give us access to the Math namespace.

But this can be inefficient if all you want to do is just arithmetic, for example. Instead of importing the entire module, you can just say:

Or, you could just not import a module at all and instead directly reference what you want to do.

It’s nothing to do with it being faster or slower in loading libraries, so don’t worry. There’s no overhead in importing modules. It’s just a shortcut. However, if you do use the shortcut, you may encounter duplicate function names between say… Unity Math namespace and the Net Math namespace which could get confusing. One solution is to comment, and comment well!

For example:

I generally recommend importing modules because it saves time having to type the full namespace each time. But it is up to you.

Let’s use everything we know in this lesson to sum it up with one example. Let’s introduce 3 integer functions which contains 3 integer arguments in its parameter:

Wow that looks so complicated! Take your time to go through each line and see what is going on. Don’t feel put off by it!

Now that you’ve read the snippet of code, let’s call those 3 functions and pass some arguments! Remember, since we specified what type of argument we want passed, in our case integers, we can only pass integers.

For biggest(int arg1, int arg2, int arg3, int arg4), I chose to pass 4,5,6,7. For smallest(int arg1, int arg2, int arg3, int arg4), I also chose to pass 4,5,6,7. Finally, for distFrom_zero(int arg), I passed -2. Here’s what we have so far on a separate function:

If we run the code, we should get the following result:

You may be thinking there must be better way to do this and you’re right! Instead of passing all those arguments, we can make use of arrays. We’ll talk more about this in our next lesson…

Well done for getting this far! This is how your code is written and read by the compiler.

--

--

Alexander Laurence
Five Minute C#

Alex graduated from the University of Edinburgh with a Masters degree in Neuroscience. He spent 3 years teaching and now runs his own tech venture.