How to create a function in C#

3 Answers

0 votes
using System;

class Program
{
    static int Square(int x) {
        return x * x;
    }
    
    static void Main() {
        int x = 10;

        Console.Write(Square(x));
    }
}




/*
run:

100

*/

 



answered Dec 21, 2022 by avibootz
0 votes
using System;

class Program
{
    static double Square(int value) => System.Math.Pow(value, 2);
    
    static void Main() {
        int x = 10;

        Console.Write(Square(x));
    }
}




/*
run:

100

*/

 



answered Dec 21, 2022 by avibootz
0 votes
using System;

class Program
{
    static int Square(int x) => (int)Math.Pow(x, 2);
    
    static void Main() {
        int x = 10;

        Console.Write(Square(x));
    }
}




/*
run:

100

*/

 



answered Dec 21, 2022 by avibootz
...