참고영상
https://youtu.be/X1rHKZAVBvw?list=PLUZ5gNInsv_O7XRpaNQIC9D5uhMZmTYAf

변수를 선언한 위치에 따라
전역변수
지역변수 (함수가 호출되면 생성됐다가 함수가 리턴 되면 소멸된다)
매개변수


 

참고영상

https://youtu.be/X1rHKZAVBvw?list=PLUZ5gNInsv_O7XRpaNQIC9D5uhMZmTYAf

 

변수를 선언한 위치에 따라

전역변수

지역변수 (함수가 호출되면 생성됐다가 함수가 리턴 되면 소멸된다)

매개변수

 

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour

{

     int a = 100; // 전역변수, 멤버 변수, 필드

     void Start()

     {

          int b = 200; // 지역변수

          print(a);

          print(b);

     }

     void Test()

     {

          print(a); // 전역변수이기 때문에 사용 가능

          print(b); // 오류 발생, 지역변수 b는 다른 지역에서는 사용 불가, 선언한 곳에서만 사용 가능

     }

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

 

 

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour

{

     int a = 100; // 전역변수, 멤버 변수, 필드

     void Start()

     {

          Test1(); //함수 호출

          Test2(); //함수 호출

     }

     void Test1()

     {

          int b = 200; // 지역변수

          print(a);

          print(b);

     }

     void Test2()

     {

          print(a); // 전역변수이기 때문에 사용 가능

                         // print(b); // 오류 발생, 지역변수 b는 다른 지역에서는 사용불가, 선언한 곳에서만 사용가능

     }

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



 

 

지역변수, 같은 이름 다른 지역

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour

{

     int a = 100; // 전역변수, 멤버 변수, 필드

     void Start()

     {

          Test1(); //함수 호출

          Test2(); //함수 호출

     }

     void Test1()

     {

          int b = 200; // 지역 변수

          print(a);

          print(b);

     }

     void Test2()

     {

          int b = 300; // 지역 변수, Test1()함수의 지역 변수 b와 지역이 달라서 오류 없이 구별 사용한다.

          print(a); // 전역변수이기 때문에 사용 가능

          print(b); //

     }

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



 

 

전역변수와 지역변수 우선순위

 

전역 변수 a, 지역 변수 a와 같이 이름이 같으면 지역 변수의 우선순위가 높다

 

참고영상

https://youtu.be/X1rHKZAVBvw?list=PLUZ5gNInsv_O7XRpaNQIC9D5uhMZmTYAf&t=134

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour

{

     int a = 100; // 전역변수, 멤버 변수, 필드

     void Start()

     {

          Test1(); //함수 호출

          Test2(); //함수 호출

     }

     void Test1()

     {

          int b = 200; // 지역변수

          print(b);

     }

     void Test2()

     {

          int b = 300; // 지역변수

          print(b); // 전역변수이기 때문에 사용 가능

     }

}



 

결과 (전역 변수 a = 100은 무시 된다)

 

200

300

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

매개변수

참고영상

https://youtu.be/X1rHKZAVBvw?list=PLUZ5gNInsv_O7XRpaNQIC9D5uhMZmTYAf&t=173

 

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour

{

     int a = 100; // 전역변수, 멤버 변수, 필드

     void Start()

     {

          Test1(100); //함수 호출

          Test2(); //함수 호출

     }

     int Test1(int abc) //abc는 매개변수다. 리턴하기 위해 함수 자료형을 int로 변경함

     {

          abc += 200; // 매개변수

          return abc;

     }

     void Test2()

     {

          int b = 300; // 지역변수

          print("Test2 = " + b); // 전역변수이기 때문에 사용 가능

     }

}



 

 




+ Recent posts