C# Dissecting Functions – Part 3

Spread the love

A quick google search of “What is a c# function?” yields this definition:
A function is a group of statements that together perform a task.

** Throughout this tutorial I use method and function interchangeably. Methods and functions do have a distinct difference that we might cover later, but for 90% of the use cases, they’ll act and behave the same way. If you want to go down this rabbit hole further, this is a great stackoverflow post about it: Method vs Function **

So before we dig into the technical parts of functions lets talk about what they allow us as programmers to do. Googles definition really tells the entire story here, we can breakup our code into smaller chunks that we can run whenever we want. Imagine you have a car that is controlled by your computer program. You code some functions for it, Forward(), Backwards(), Stop(), and so forth. If you need to make the car move, we just call our function Forward() and assuming the code is correct the car should move Forward(). When we need to Stop we call the Stop() function. It should be noted that functions are created inside a class or object. The most important concept here is encapsulation, functions allow us to encapsulate code for later use. Other functions and code need not know whats inside or how it works, it doesn’t care. Functions are also a really good way to breakup your code and organize it for maintainability and readability.

Lets take a peak at how to construct a C# function.

//accessLevel returnValue methodName (method paramaters)

This is called a method signature, each of the components has different options that dictate how that function will behave.

Access level or sometimes called access modifier controls where this function can be called from. Here’s are all the access levels you’re able to define:

Return Type tells the function what type of object or primitive it HAS to return when the function ends. So if we had a function that looked like this:

 public int AddTwoNumbers() 
 {
   int valueOne = 1;
   int valueTwo = 2;
   sum = valueOne + valueTwo;
   return sum;
 }

We can tell right from the start that its public and accessible anywhere in our assembly. The int statement tells us that this method returns an integer. If we step through the code we can see that sum is made up of adding valueOne and valueTwo. Finally, we use the “return” command and provide our int value “sum”.

Method Name is the name you’ll be giving your function, different languages have different coding conventions. However, C# typically sticks to some convention of camel case or pascal. The function name should be descriptive and let the reader know what it is doing. Functions should only do one task. If you have two or more tasks inside a function, you should consider breaking each of those into separate functions.

Method Parametersallow us as programmers to pass values into methods. So if we revisit the AddTwoNumbers() function from above, we could modify it like this:

public int AddTwoNumbers(int valueOne, int valueTwo) 
{
  sum = valueOne + valueTwo;
  return sum;
}

With this function we’re now passing the two values into the function, then adding those numbers together, and finally returning the sum. This function is much more versatile as we can now provide any two integer values, where as before we were limited to only adding 1+3. Functions can take pretty much any data type as a parameter. Some examples would be:
all the primitives
arrays, lists, dictionaries, custom objects.

So try to write a couple functions on your own.

Challenge:write a function that takes in a string parameter “personName” and outputs that name to the console. Post a picture of your code here.

Happy Coding!

Corey

Leave a Reply

Your email address will not be published. Required fields are marked *