using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FunctionEx : MonoBehaviour
{
    // 리턴값이 없음
    // return을 사용하는 순간 함수 종료
    public void VoidFunction()
    {
        Debug.Log(message: "A");
        return;
        Debug.Log(message: "B"); // 출력되지 않음
    }

    public int MinusFunction(int x, int y)
    {
        return x-y;
    }

    public int[] Calculation1(int x, int y)
    {
        int result1 = x + y;
        int result2 = x - y;
        int result3 = x * y;
        int result4 = x / y;

        int[] result = { result1, result2, result3, result4 };

        return result;
    }

    public float[] Calculation2(float x, float y)
    {
        float result1 = x + y;
        float result2 = x - y;
        float result3 = x * y;
        float result4 = x / y;

        float[] result = { result1, result2, result3, result4 };

        return result;
    }
    
    public int Calculation3(int x, int y)
    {
        int result = 1;

        for(int i=0; i < y; i++)
        {
            result *= x;
        }

        return result;
    }

    private void Awake()
    {
        VoidFunction(); // 함수만 () 사용
        int result = MinusFunction(5, 3);
        Debug.Log(message: "마이너스 계산 결과1 : " + result);

        Debug.Log("마이너스 계산 결과2 : " + MinusFunction(5, 3));

        Debug.Log($"마이너스 계산 결과3 : {MinusFunction(5, 3)}"); // 문자열 보간법

        for(int i=0; i<4; i++)
        {
            Debug.Log($"정수형 계산 결과 : {Calculation1(5, 3)[i]}");
        }

        for (int i = 0; i < 4; i++)
        {
            Debug.Log($"실수형 계산 결과 : {Calculation2(5, 3)[i]}");
        }
        
        Debug.Log("x의 y제곱 값 :" + Calculation3(2, 5));
        
        Debug.Log($"제곱값 구하는 함수 : {Mathf.Pow(2, 5)}"); //
    }
}

 

Mathf.Pow()

제곱 값 구할 때 유니티에서는 Mathf.Pow() 함수 쓰면 된다.

 

문자열 보간법

프로그램 실행 중 문자열 내에 변수 또는 상수의 실질적인 값을 표현하기 위해 사용

$로 시작한 후 {}로 감싸서 사용한다.

Debug.Log($"사과는 {x}개");

+ Recent posts