일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- mvc
- pypy3
- Java
- Python
- 소수찾기
- appendleft
- deque
- DP
- spring
- 프로그래머스
- BFS
- 1일1솔
- 누적합
- 연관관계
- 소수판별
- 완전탐색
- c#
- popleft
- JPA
- 브루투포스
- 우선순위큐
- unity
- 합 구하기
- python3
- 인프런
- 파이썬
- C#강의
- 그리디 알고리즘
- 백준
- LCM
Archives
- Today
- Total
jae_coding
[유니티로 배우는 C#] 함수 본문
반응형
함수의 기능
예제 함수
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unity_lecture : MonoBehaviour
{
int intValue;
float floatValue = 10.1f;
void FloatToInt(){
intValue = (int)floatValue;
}
void Start()
{
FloatToInt();
print(intValue);
}
// Update is called once per frame
void Update()
{
}
}
예제의 코드에서는 함수가 1번밖에 사용되어지지 않았지만, 함수가 여러변 그리고 함수의 내용이 길게 된다면 반복되는 작업을 간편하게 코드가 구현될 수 있도록 도와줄 수 있다.
매개변수가 있는 함수
float floatValue = 10.1f;
float2 floatValue2 = 20.5f;
void FloatToInt(float _parameter){
intValue = (int)_parameter;
print(intValue);
}
void Start()
{
FloatToInt(floatValue); //10출력
FloatToInt(floatValue2); //20출력
}
입력을 받아 처리하는 함수를 만들 수 있다.
디폴트가 있는 함수
float floatValue = 10.1f;
float2 floatValue2 = 20.5f;
void FloatToInt(float _parameter, string _stringParm = "default){
intValue = (int)_parameter;
print(intValue);
print(_stringParm);
}
void Start()
{
FloatToInt(floatValue, "Test"); //10, Test출력
FloatToInt(floatValue2); //20, default출력
}
반환값이 있는 함수
int FloatToInt(float _parameter, float _parameter2){
intValue = (int)(_parameter + _parameter2);
print(intValue);
return intValue;
}
void Start()
{
Value = FloatToInt(floatValue, floatValue2);
print(Value);
}
함수의 이름을 선언하기전에 반환을 할 타입을 먼저 결정해야한다. 위 코드의 경우에는 Integer type을 반환하여 주기위해서 FloatToInt함수를 int타입으로 return해주는 함수를 만든 것이다.
강의자료: 케이디 유튜브
반응형
'C#' 카테고리의 다른 글
[유니티로 배우는 C#] 컬렉션 (0) | 2022.07.19 |
---|---|
[유니티로 배우는 C#] 배열 (0) | 2022.07.19 |
[유니티로 배우는 C#] 접근 지정자 & 범위 지정자 (0) | 2022.07.19 |
[유니티로 배우는 C#] 자료형 (0) | 2022.07.19 |
[유니티로 배우는 C#] 변수 (0) | 2022.07.19 |
Comments