Creating a Function to Add Two Numbers

Step 1: Define the Function

In C#, defining a function involves specifying its return type, name, and parameters inside parentheses. Here’s how you can define a function to add two numbers:

C#
using System;

public class Program
{
    public static void Main()
    {
        // Example usage:
        int result = AddNumbers(5, 3);
        Console.WriteLine("Sum is: " + result);
    }
    
    // Function to add two numbers
    public static int AddNumbers(int a, int b)
    {
        int sum = a + b;
        return sum;
    }
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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