Defining Functions - potatoscript/csharp GitHub Wiki

πŸ› οΈ Defining Functions in C# πŸ› οΈ

πŸ§‘β€πŸ’» What Is a Function? πŸ§‘β€πŸ’»

A function is like a little helper that performs a task for you in your program. Instead of writing the same code over and over again, you can define a function and then call it whenever you need it. Functions help make your code neat, reusable, and easier to manage.

Think of it like a recipe! πŸ§‘β€πŸ³ When you follow a recipe to bake a cake, you're not repeating the steps each time. You just follow the recipe and get the cake every time. A function is the recipe, and your program is the cake! πŸŽ‚


πŸ§‘β€πŸ’» Defining a Function in C# πŸ§‘β€πŸ’»

Here’s how to define a simple function in C#:

🏷️ Function Syntax:

returnType FunctionName(parameters)
{
    // Code to perform the task
}
  • returnType: This is the type of value the function will return (like int, string, void).
  • FunctionName: This is the name you give to the function (just like the name of a recipe).
  • parameters: These are the values that the function needs to work with (like ingredients in a recipe).

πŸ§‘β€πŸ’» A Basic Example: Saying Hello (Void Function) πŸ§‘β€πŸ’»

Let’s start with a very simple function that doesn’t return anything. We’ll call it SayHello.

🏷️ Code:

using System;

class Program
{
    static void Main()
    {
        SayHello(); // Calling the SayHello function
    }

    // Defining a function to say hello
    static void SayHello()
    {
        Console.WriteLine("Hello, welcome to C#!"); // This function just prints a message
    }
}

Explanation:

  • static void SayHello(): This defines a function called SayHello. It has no return value (that’s why we use void).
  • Inside the function, we have `Console.WriteLine("Hello, welcome to C#!") which just prints a message to the screen.
  • We call the function inside the Main method using SayHello();.

When you run this program, it will print:

Hello, welcome to C#!

πŸ§‘β€πŸ’» Returning a Value from a Function πŸ§‘β€πŸ’»

You can also define functions that return a value. For example, let’s define a function that adds two numbers together and returns the result.

🏷️ Code: Adding Two Numbers (Function with Return Value)

using System;

class Program
{
    static void Main()
    {
        int result = AddNumbers(3, 4); // Calling the function and storing the result
        Console.WriteLine("The sum is: " + result); // Printing the result
    }

    // Defining a function that returns the sum of two numbers
    static int AddNumbers(int num1, int num2)
    {
        return num1 + num2; // Adding num1 and num2 and returning the result
    }
}

Explanation:

  • static int AddNumbers(int num1, int num2): This defines a function called AddNumbers. It takes two parameters, num1 and num2, which are the numbers you want to add.
  • The function returns an int (integer) value, which is the sum of num1 and num2.
  • We call the function in the Main method, passing in 3 and 4, and store the result in the variable result.

When you run this program, it will print:

The sum is: 7

πŸ§‘β€πŸ’» Using Parameters in Functions πŸ§‘β€πŸ’»

Parameters are like ingredients in a recipe. You tell the function what you want it to work with by passing values when you call it.

🏷️ Code: Making a Greeting Function (With Parameters)

using System;

class Program
{
    static void Main()
    {
        GreetUser("Lucy"); // Passing the name "Lucy" to the function
        GreetUser("John"); // Passing the name "John" to the function
    }

    // Defining a function that greets a user by their name
    static void GreetUser(string name)
    {
        Console.WriteLine("Hello, " + name + "! Welcome to the world of C#!");
    }
}

Explanation:

  • The function GreetUser takes a string parameter called name.
  • When you call GreetUser("Lucy"), it passes the name "Lucy" to the function.
  • The function prints out "Hello, Lucy! Welcome to the world of C#!".

When you run this program, it will print:

Hello, Lucy! Welcome to the world of C#!
Hello, John! Welcome to the world of C#!

πŸ§‘β€πŸ’» Function Overloading πŸ§‘β€πŸ’»

Function Overloading means that you can define multiple functions with the same name, but with different parameters. C# will know which one to use based on the parameters you provide.

🏷️ Code: Function Overloading (Same Name, Different Parameters)

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(AddNumbers(3, 4)); // Calling AddNumbers with two integers
        Console.WriteLine(AddNumbers(3.5, 4.5)); // Calling AddNumbers with two doubles
    }

    // Function that adds two integers
    static int AddNumbers(int num1, int num2)
    {
        return num1 + num2;
    }

    // Function that adds two doubles
    static double AddNumbers(double num1, double num2)
    {
        return num1 + num2;
    }
}

Explanation:

  • Both functions are named AddNumbers, but one adds integers (int), and the other adds floating-point numbers (double).
  • C# automatically chooses the correct function based on the type of values passed when you call it.

When you run this program, it will print:

7
8

πŸ§‘β€πŸ’» Returning Multiple Values from a Function πŸ§‘β€πŸ’»

Sometimes, you might want to return more than one value from a function. While C# doesn’t allow returning multiple values directly, we can use techniques like out parameters or tuples to return multiple values.

🏷️ Code: Returning Multiple Values Using Tuples

using System;

class Program
{
    static void Main()
    {
        var result = GetFullName("John", "Doe"); // Using a tuple to get both first and last name
        Console.WriteLine("Full Name: " + result.Item1 + " " + result.Item2); // Accessing tuple items
    }

    // Function that returns multiple values using a tuple
    static (string, string) GetFullName(string firstName, string lastName)
    {
        return (firstName, lastName); // Returning both first and last names as a tuple
    }
}

Explanation:

  • The function GetFullName returns a tuple, which is a pair of values: the first name and the last name.
  • We access the values using result.Item1 for the first name and result.Item2 for the last name.

When you run this program, it will print:

Full Name: John Doe