Answers for "writing functions 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
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
0

functions in c#

using UnityEngine;

public class Gun : MonoBehaviour
{

    public float damage = 10f;
    public float range = 100f;

    public Camera fpsCam;
    public ParticleSystem muzzleFlash;

    // Update is called once per frame
    void Update()
    {

        if (Input.GetButtonDown("Fire1"))
        {
            // Call function
            Shoot();
        }

    }

    // Declare & Program function
    void Shoot ()
    {

        muzzleFlash.Play();

        RaycastHit hit;

        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            Debug.Log(hit.transform.name);

            Target target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
                target.TakeDamage(damage);
            }
        }
    }
}
Posted by: Guest on July-14-2021

C# Answers by Framework

Browse Popular Code Answers by Language