메소드와 변수의 스코프

Download Report

Transcript 메소드와 변수의 스코프

Chapter 06. 메소드와 변수의 유효범위
1
06-1. 메소드 (Methods)
2
메소드 (Method)
Method – 작업을 수행하기 위한 명령문의 집합
메소드 선언 (Method declaration)
public static void main(String[] args) {
int i = 1;
int j = 2;
System.out.println(i + " + " + j + " = " + (i + j));
}
3
메소드 선언
메소드 선언
public static void announceWeather( )
{
System.out.println("좋은 아침입니다.");
System.out.println("봄이 왔습니다.");
}
4
메소드 호출
class MethodDemo
{
public static void main(String[] args) {
System.out.println("프로그램 시작\n");
announceWeather( );
System.out.println();
announceWeather( );
System.out.println("\n프로그램 끝");
}
프로그램 시작
좋은 아침입니다.
봄이 왔습니다.
좋은 아침입니다.
봄이 왔습니다.
프로그램 끝
메소드 선언
public static void announceWeather( )
{
System.out.println("좋은 아침입니다.");
System.out.println("봄이 왔습니다.");
}
}
5
매개변수 (parameter)
class MethodDemo
{
public static void main(String[] args) {
System.out.println("프로그램 시작\n");
announceWeather( 10);
System.out.println();
announceWeather( 15);
System.out.println("\n프로그램 끝");
}
프로그램 시작
좋은 아침입니다.
기온은 10도입니다.
좋은 아침입니다.
기온은 15도입니다.
프로그램 끝
메소드 선언
public static void announceWeather( int degree) {
System.out.println("좋은 아침입니다.");
System.out.println("기온은 " + degree + "도 입니다.");
}
}
6
매개변수 (parameter)
인자 (argument)
announceWeather( 10);
값을 복사(copy)한다.
매개변수 (parameter)
메소드 선언
public static void announceWeather( int degree) {
System.out.println("좋은 아침입니다.");
System.out.println("기온은 " + degree + "도 입니다.");
}
7
여러 개의 매개변수 (parameter)
class MethodDemo
{
public static void main(String[] args) {
System.out.println("프로그램 시작\n");
announceWeather(10, 20);
System.out.println();
announceWeather(15, 30);
System.out.println("\n프로그램 끝");
}
프로그램 시작
좋은 아침입니다.
최저 10도, 최고 20도입니다.
좋은 아침입니다.
최저 15도, 최고 30도입니다.
프로그램 끝
메소드 선언
public static void announceWeather(int low, int high) {
System.out.println("좋은 아침입니다.");
System.out.println("최저 " + low + "도, 최고 " + high " + "도입니다.");
}
}
8
변수도 인자가 될 수 있다.
class MethodDemo
{
public static void main(String[] args) {
int l = 10;
int j = 20;
announceWeather(i, j);
}
좋은 아침입니다.
최저 10도, 최고 20도입니다.
메소드 선언
public static void announceWeather(int low, int high) {
System.out.println("좋은 아침입니다.");
System.out.println("최저 " + low + "도, 최고 " + high " + "도입니다.");
}
}
9
인자와 매개변수는 같은 타입이어야 한다.
public static void main(String[] args) {
intWeather(10, 20);
doubleWeather(10.5, 20.1);
}
public static void intWeather(int low, int high) {
System.out.println("좋은 아침입니다.");
System.out.println("최저 " + low + "도, 최고 " + high " + "도입니다.");
}
public static void doubleWeather(double low, double high) {
System.out.println("좋은 아침입니다.");
System.out.println("최저 " + low + "도, 최고 " + high " + "도입니다.");
}
좋은 아침입니다.
최저 10도, 최고 20도입니다. 좋은
아침입니다.
최저 10.5도, 최고 20.1도입니다.
10
인자가 매개변수에 복사될 때 자동형변환이
일어날 수 있다.
public static void main(String[] args) {
doubleWeather(10, 20);
}
public static void doubleWeather(double low, double high) {
System.out.println("좋은 아침입니다.");
System.out.println("최저 " + low + "도, 최고 " + high " + "도입니다.");
}
좋은 아침입니다.
최저 10.0도, 최고 20.0도입니다.
11
인자가 매개변수에 복사될 때 컴파일 에러
public static void main(String[] args) {
intWeather(10.5, 20.1);
}
public static void intWeather(int low, int high) {
System.out.println("좋은 아침입니다.");
System.out.println("최저 " + low + "도, 최고 " + high " + "도입니다.");
}
12
값을 반환하는 메소드
class MethodDemo
{
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println("합은 " + sum);
System.out.println("합은 " + add(10, 20));
}
합은 30
합은 30
메소드 선언
public static int add(int operand1, int operand2) {
int result = operand1 + operand2;
return result;
}
}
13
값을 반환하는 메소드
class MethodDemo
{
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println("합은 " + sum);
System.out.println("합은 " + add(10, 20));
}
메소드 선언
public static int add(int operand1, int operand2) {
int result = operand1 + operand2;
return result;
}
}
반환값의 타입 (return type)
result는 int 타입이어야 한다.
14
값을 반환하지 않는 메소드
public static void main(String[] args) {
doubleWeather(10.5, 20.1);
}
public static void doubleWeather(double low, double high) {
System.out.println("좋은 아침입니다.");
System.out.println("최저 " + low + "도, 최고 " + high " + "도입니다.");
}
아무런 값도 반환하지 않는다는 의미
15
키워드 return이 지니는 두 가지 의미
실행결과
divide 메소드의 종료만을 의미
함
값의 반환, 메소드의 종료, 이렇게 두 가지의 의미를 지님
16
06-2. 변수의 유효범위
(스코프, scope)
17
지역변수
• 메소드 내부에 선언된 변수를 지역변수라고 한다.
• 지역변수의 유효범위(scope):
변수가 선언된 곳부터 현재 블록의 끝까지
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println("합은 " + sum);
System.out.println("합은 " + add(10, 20));
}
18
지역변수의 유효범위
class MethodDemo
{
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println("합은 " + sum);
System.out.println("합은 " + add(10, 20));
}
public static int add(int operand1, int operand2) {
int result = operand1 + operand2;
return result;
}
}
19
지역변수의 이름이 같아도 문제 없다.
class MethodDemo
{
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println("합은 " + sum);
System.out.println("합은 " + add(10, 20));
}
public static int add(int operand1, int operand2) {
int sum = operand1 + operand2;
return sum;
}
}
20
매개변수(parameter)도 지역변수의 일종
class MethodDemo
{
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println("합은 " + sum);
System.out.println("합은 " + add(10, 20));
}
public static int add(int operand1, int operand2) {
int sum = operand1 + operand2;
return sum,;
}
}
유효범위
21
변수 num을 선언함
변수 num을 새로 선언함
num의 유효범위
num의 유효범위
변수 scope의 유효
범위
num=3의 유효범위
메소드 실행이 끝나면 그 메소드 내에서 만들어진 지역변수는 소멸된다.
22
23
06-3. 메소드 재귀호출
(Recursion)
24
재귀 (Recursion)
25
재귀 (Recursion)
26
27
28
지역변수 n=3
매개변수도 지역변수이다.
같은 것이 아님
지역변수 n=2
메소드가 호출될 때마다
새 지역변수(매개변수)가 만들어진다.
메소드 내에서 만들어진 지역변수는
메소드가 종료될 때 소멸된다.
지역변수 n=1
종료조건
29
class Recursion {
public static void main(String[] args)
{
showHi(3);
}
public static void showHi(int cnt)
{
System.out.println(cnt);
if(cnt==1)
return;
showHi(--cnt);
}
}
showHi(int cnt) {
System.out.println(cnt);
if(cnt==1)
cnt=3
return;
showHi(--cnt);
}
호출
showHi(int cnt) {
System.out.println(cnt);
if(cnt==1)
cnt=2
return;
showHi(--cnt);
}
호출
showHi(int cnt) {
System.out.println(cnt);
if(cnt==1)
cnt=1
return;
showHi(--cnt);
}
30
class Recursion {
public static void main(String[] args)
{
showHi(3);
}
public static void showHi(int cnt)
{
System.out.println("in"+cnt);
if(cnt==1)
return;
showHi(--cnt);
System.out.println("out"+cnt);
}
}
showHi(int cnt) {
System.out.println("in"+cnt);
if(cnt==1)
cnt=3
return;
showHi(--cnt);
System.out.println("in"+cnt);
}
showHi(int cnt) {
System.out.println("in"+cnt);
if(cnt==1)
cnt=2
return;
showHi(--cnt);
System.out.println("in"+cnt);
}
showHi(int cnt) {
System.out.println("in"+cnt);
if(cnt==1)
cnt=1
return;
showHi(--cnt);
System.out.println("in"+cnt);
}
31
class Recursion {
public static void main(String[] args)
{
showHi(3);
}
public static void showHi(int cnt)
{
System.out.println("Hi~ ");
showHi(--cnt);
if(cnt==1)
return;
}
}
?
showHi(int cnt) {
System.out.println("Hi~ ");
showHi(--cnt);
cnt=3
if(cnt==1)
return;
}
호출
showHi(int cnt) {
System.out.println("Hi~ ");
showHi(--cnt);
cnt=2
if(cnt==1)
return;
}
호출
showHi(int cnt) {
System.out.println("Hi~ ");
showHi(--cnt);
cnt=1
if(cnt==1)
return;
}
32
Recursive Summation
public class RecursiveSum {
public static void main(String[] args) {
System.out.println(sum(5));
}
static int sum(int value)
{
if (value == 0)
return 0;
else
return value + sum(value - 1);
}
}
33
끝.
34