tnas
5
Q:

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;
	}
}
1
public int AddNumbers(int number1, int number2){    int result = number1 + number2;    if(result > 10)    {    return result;    }    return 0;}
3
public void SayHello()
{
Console.WriteLine("Hello") ;
}
//as basic as it gets 
//for a function
1
public void SayHello(string name) 
{
    Console.WriteLine("Hello");
}

public void SayName()
{
	Console.WriteLine("What is your name?");
	string name = Console.ReadLine(); 
	SayHello(name);
}
0
//function example
using System;
					
public class Program
{
	static void function(){
		Console.WriteLine("I am a function!");
	}
	public static void Main()
	{
		function();
	}
}
0

New to Communities?

Join the community