c-sharp
  1. c-sharp-function

C# Function

A function is a block of code that can be called from other parts of a program to perform a specific task. In C#, a function is defined using the "function" keyword, followed by the function name, parameters (if any), and the function body enclosed in curly braces. In this tutorial, we'll discuss how to create and use functions in C#.

Syntax

To create and use a function in C#, follow these steps:

  1. Define the function using the "function" keyword, followed by the function name and parameters (if any).
  2. Write the code for the function in the function body.
  3. Call the function from another part of the program, passing in any required arguments.

Example

Let's say you want to write a C# program that performs some basic arithmetic calculations. You can create a function for each calculation type and call them as needed. Here's an example program that uses functions:

using System;

class Program
{
    static void Main()
    {
        int num1 = 5, num2 = 10;

        Console.WriteLine("The sum of {0} and {1} is {2}", num1, num2, Add(num1, num2));
        Console.WriteLine("The difference between {1} and {0} is {2}", num1, num2, Subtract(num1, num2));
        Console.WriteLine("The product of {0} and {1} is {2}", num1, num2, Multiply(num1, num2));
        Console.WriteLine("The quotient of {1} and {0} is {2}", num1, num2, Divide(num1, num2));
    }

    static int Add(int a, int b)
    {
        return a + b;
    }

    static int Subtract(int a, int b)
    {
        return a - b;
    }

    static int Multiply(int a, int b)
    {
        return a * b;
    }

    static int Divide(int a, int b)
    {
        return a / b;
    }
}

Output

The output of the above example program is:

The sum of 5 and 10 is 15
The difference between 10 and 5 is 5
The product of 5 and 10 is 50
The quotient of 10 and 5 is 2

Explanation

In the above example, we created four functions - Add(), Subtract(), Multiply(), and Divide() - each of which performs a specific arithmetic operation. We then called these functions in the Main() method of the program, passing in the required arguments and using the Console.WriteLine() method to print the results to the console.

Use

Functions in C# can be used to break down a large program into smaller, more manageable pieces. Functions can be used to perform common operations, such as arithmetic calculations, string manipulation, and file input/output.

Important Points

  • Functions should be given descriptive names that reflect their purpose.
  • Functions can return a value or perform an action.
  • Functions can take zero or more parameters.
  • The scope of a variable defined inside a function is limited to that function.

Summary

In this tutorial, we discussed how to create and use functions in C#. We covered the syntax, example, output, explanation, use, important points, and summary of functions in C#. With this knowledge, you can now create custom functions for your C# programs, and reuse code to save time and reduce errors.

Published on: