Answers for "how to create a function in c#"

C#
3

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;
	}
}
Posted by: Guest on November-07-2020
1

functions in c#

using System;
					
public class Program
{
	public static void Main()
	{
		//functions can have retun type name and optional parameters 
		Console.WriteLine("-------functions---------");
		float MilesToKm(float miles){
			float km = miles*1.6f ; 
			return km ;
		}
		//calling a function
		Console.WriteLine(MilesToKm(3.0f));
		
		
		
	}
}
Posted by: Guest on November-02-2021
6

c# funtion

public int AddNumbers(int number1, int number2){    int result = number1 + number2;    if(result > 10)    {    return result;    }    return 0;}
Posted by: Guest on November-25-2019
2

c# function

public void SayHello()
{
Console.WriteLine("Hello") ;
}
//as basic as it gets 
//for a function
Posted by: Guest on February-17-2020
0

function in c#

//function example
using System;
					
public class Program
{
	static void function(){
		Console.WriteLine("I am a function!");
	}
	public static void Main()
	{
		function();
	}
}
Posted by: Guest on March-30-2020
0

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)
Posted by: Guest on April-19-2021

Code answers related to "how to create a function in c#"

C# Answers by Framework

Browse Popular Code Answers by Language