- 함수(function)란 하나의 특별한 목적의 작업을 수행하기 위해 독립적으로 설계된 코드의 집합으로 그 처리 로직을 재실행 , 재사용, 반복사용 가능
이미지참조: https://evan-moon.github.io/2019/12/29/about-pure-functions/
메서드(method) 또는 멤버 함수(member function)
- 객체지향 프로그래밍에서의 함수
- 자바의 클래스 = 멤버변수 + 멤버메서드로 구성
- 메서드(함수) 중요 구성요소: 인자(argument)와 리턴 값
- 함수 사용(호출)시 무엇을 주고(전달) 무엇을 받을지(리턴)를 생각하는 것이 중요
형식
접근자 리턴데이터타입 함수명(인자 x) {
처리 로직
결과 리턴
}
public int fn(int x) {
int y = x * x;
return y
}
메서드 만들기
리턴값이 있는 경우
- 리턴값 자료형과 같은 타입 사용
package chapter05;
public class MethodEx {
public static int add(int x) {
int y = x * x;
return y;
}
public static void main(String[] args) {
}
}
리턴값이 없는 경우
- 리턴타입 대신에 void 사용
package chapter05;
public class MethodEx {
public static int add(int x) {
int y = x * x;
return y;
}
public static void addPrint(int x) {
int y = x * x;
print(y)
}
public static void main(String[] args) {
}
}
메서드 사용
메서드가 동일 클래스(파일)에 정의 되어 있는 경우
package chapter05;
import java.util.Scanner;
public class MethodEx {
public static int add(int x) {
int y = x + x;
return y;
}
public static void main(String[] args) {
int x = 10;
int y = add(x);
System.out.println("y = add(x) = " + y);
x = 20;
y = add(x);
System.out.println("y = add(x) = " + y);
System.out.println("int input > ");
Scanner scan = new Scanner(System.in);
x = scan.nextInt();
y = add(x);
System.out.println("y = add(x) = " + y);
}
}
메서드가 다른 클래스(파일)에 정의 되어 있는 경우
메서드 정의
package chapter05;
public class MyMath {
// static 메서드
public static int add(int x) {
int y = x + x;
return y;
}
// instance 메서드
public int add(int x1, int x2) {
int y = x1 + x2;
return y;
}
}
메서드 사용(호출)
package chapter05;
public class MyMathTest {
public static void main(String[] args) {
int x1 = 10;
int x2 = 20;
// 다른 클래스 static 메서드 호출
int y1 = MyMath.add(x1);
// 다른 클래스 instance 메서드 호출
MyMath myMath = new MyMath();
int y2 = myMath.add(x1, x2);
System.out.printf("(y1,y2) = (%s,%s)", y1, y2);
}
}
메서드 중지 - return
- 메서드를 중지하고 빠저나오기
- return문 하위의 실행문은 실행되지 않음
package chapter05;
import java.util.Scanner;
public class MethodEx {
public static void addPrint(int x) {
if(x == 0) {
System.out.println("end");
return;
}
int y = x + x;
System.out.println("y="+ y);
}
public static void main(String[] args) {
addPrint(0);
}
}
지역변수 범위 scope
package chapter05;
public class LocalVarTest {
public static void main(String[] args) {
int n = 0;
{
int a = 10;
}
// int b = a; // error 블럭에서 선언된 a 지역변수
for(int i=0; i<3; i++) {
int j = 3;
}
// int b = i; error for() 선언된 i, for블럭에 선언된 j 지역변수
}
public static void nUse() {
//int k = n; // main()에 선언된 n 지역변수
}
}
메서드 인자 전달 방법
- 기본형 변수에 값 전달 => 복사본(지역변수) => 전달값 변화 없음
- 참조형 변수에 주소 전달 => 실제값(지역변수) => 전달값에 영향이 미침
package chapter05;
public class ArgsTest {
public static void varInt(int x) {
x = 10;
}
public static void varArr(int[] x) {
x[0] = 10;
}
public static void main(String[] args) {
int x = 0;
// 기본형 변수에 값 전달 => 복사본 => 전달값 변화 없음
varInt(x);
int[] xArr = {0};
//참조형 변수에 주소 전달 => 실제값 => 전달값에 영향이 미침
varArr(xArr);
System.out.println(x);
System.out.println(xArr[0]);
}
}
깃 허브 주소 : https://github.com/bjw5035/Academy_Study
'Programing > JAVA' 카테고리의 다른 글
클래스 CLASS_Part1 (0) | 2022.03.02 |
---|---|
배열 ARRAY (2) | 2022.02.28 |
제어문 - 반복문 LOOP (1) | 2022.02.23 |
제어문 - 조건문 CONDITIONAL (1) | 2022.02.21 |
연산 (0) | 2022.02.21 |