how are functions called in c # code example
Example 1: c# functions
/* Answer to: "c# functions" */
/*
A function has the following syntax:
<modifiers> <return-type> method-name(parameter-list)
You can use the following modifiers with a local function:
- async
- unsafe
- static (in C# 8.0 and later). A static local function can't capture local
variables or instance state.
- extern (in C# 9.0 and later). An external local function must be static.
There's an example below:
*/
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(square(4)); // Returns '16'
Console.WriteLine(cube(4)); // Returns '64'
}
public static int square(int n)
{
return n * n;
}
public static int cube(int n)
{
return n * n * n;
}
}
Example 2: how to define a function in c#
//The "void" in this instance refers to the return type of this function
//This function won't have a return value.
void functionName(int parameter) {
//Code you want to run
}
//Calls the code in the function
functionName(Aninteger)