Skip to content

B: Functions

Concepts in Action

Functions provide a way to break down the game’s logic into smaller, reusable, and manageable chunks of code. They promote code organization, readability, and maintainability. Functions can be called and executed when needed. Additionally, functions facilitate code reuse, as the same function can be called from different parts of the game or across different levels.

By utilizing functions effectively, you can develop a well-structured, modular, and maintainable project with organized code that is easier to understand, test, and extend.

Introduction

Functions in C# are blocks of code designed to perform a particular task. They’re a fundamental building block in the language, allowing you to create reusable, modular code.

Imagine that a book is your whole program. Just like a book is divided into chapters, your program is divided into different classes. And just like chapters contain paragraphs, classes contain functions.

A paragraph in a book has a specific purpose. It could be to describe a character, explain an event, or convey a particular emotion. Similarly, a function in C# has a specific purpose. It might calculate a value, process some data, or control a game character.

The sentences in a paragraph make up the content of the paragraph. In a function, the statements (lines of code) make up the body of the function.

Prior C# knowledge required

Understanding of C# types is necessary to follow along with this article. If you need a refresher, you can review our C# Unveiled: Types article.

Definition

In C#, a function is defined with its return type, followed by the name, parentheses () to specify that it is a function, and its content enclosed by {} braces. The name of the function is defined by the developer, so make sure that the name of the gives the user enough information about what it is the function does. Inside the parentheses, you can define parameters that the function will accept when it is called. These are needed by the function to allow it to do its purpose.

In C#, functions are typically declared within a class, and they define the behaviors that an object of that class can perform. The location of a function declaration within a class is flexible - it can be at the beginning, middle, or end of the class, but it must be within the class’s curly braces {}. More information about classes and functions can be found in C# Unveiled: Classes

Differences to JavaScript

In JavaScript, a function is a standalone entity, and can be defined outside of any specific object or class. It has a name, a list of parameters, and a body of code. In C#, you typically need an instance of the class to call a function. For example, you first need to create a cat instance from the cat class blueprint to be able to do anything with it. This is not always the case in JavaScript.

In C#, each function has a declared return type. If it doesn’t return a value, it’s marked as void.

In JavaScript, a function doesn’t have a declared return type. If no value is returned from the function, it implicitly returns undefined.

Similarities to JavaScript

Both JavaScript and C# share some similar data types like boolean, string, and numerical types.

Despite their differences, C# and JavaScript share several similarities when it comes to functions:

  • Both use parentheses () to enclose parameters and curly braces {} to enclose the function body.
  • Both allow functions to take parameters and return values. Both allow default values for parameters (though the syntax is different).

Examples

//This function does not need any parameters to perform its task. It also doesn't have a value that it gives back when it is done working. It simply performs an action (Writing some code to the Console) and is then done.
public void SayHello() 
{
    Debug.Log("Hello, World!");
}

//This function does not need any parameters to perform its task. It does however give a value back when it is done. In this case, the original caller will receive an integer value that can be stored into a variable and used later.
public int GetYear() 
{
    return 2023;
}

//This function needs two parameters to perform its task. Specifically two integers that will be added together. It will then give back the value to the original caller, which receives an integer value that can be stored into a variable and used later.
public int Add(int a, int b) 
{
    int result = a + b;
    return result;
}

//To call these functions, simply write the name of the function and use the parentheses to specify you want to call the function with that name: 
SayHello();             // Outputs: "Hello, World!"
int year = GetYear();   // year is now 2023
int sum = Add(5, 7);    // sum is now 12

Key Takeaways

  • In C#, functions (known as methods) are part of classes, similar to how paragraphs are part of a chapter in a book.
  • Calling a method in C# typically requires an instance of the class, which is like referring to a specific copy of a book to read a paragraph.
  • Every C# method has a declared return type, even if it’s void, which signifies no return value. This is like knowing in advance whether a paragraph will provide you with a specific piece of information or not.

Further Reading

C# Programming Guide - Methods


Last update: May 16, 2023