main sub main function A function B Return
주(main) 함수와 부(sub) 함수 main() function. A() function. B() Return; CALLING function. B() Return; CALLED CALLING CALLED
(예) 리턴 값이 없을 경우 void function. A() { cout << "₩n smile(^&^)"); return; }; (예) return 값이 있을 경우 int function. B(int n) { int i, total=0; for(i=1; i<=n; i++) total = total + i; return total; }; SQ Lab. 7
(예) 함수 /*001*/ // function 01 c. Ncpp /*002*/ /*003*/ #include "iostream" /*004*/ /*005*/ using namespace std; /*006*/ /*007*/ ////////////////////// /*008*/ void function 01() /*009*/ { /*010*/ cout << "₩n hi(^@^)"; /*011*/ } /*012*/ ////////////////////// /*013*/ int function 02(int x, int y) /*014*/ { /*015*/ return (x+y); /*016*/ } /*017*/ ////////////////////// SQ Lab. 10
/*018*/ void main() /*019*/ { /*020*/ void function 03(); /*021*/ /*022*/ function 01(); /*023*/ /*024*/ int result; /*025*/ result = function 02(10, 20); /*026*/ cout << "₩n₩n result=>" << result; /*027*/ /*028*/ function 03(); /*029*/ /*030*/ cout << "₩n₩n Type any character : "; /*031*/ cin >> result; /*032*/ } /*033*/ ////////////////////// SQ Lab. 11
/*034*/ void function 03() /*035*/ { /*036*/ cout << "₩n₩n Bye bye^^"; /*037*/ } SQ Lab. 12
[결 과] SQ Lab. 13
(예) 값 전달 방식(call by value) /*001*/ // function 10 c. Ncpp /*002*/ /*003*/ #include "iostream" /*004*/ /*005*/ using namespace std; /*006*/ /*007*/ void swap 01 value(int x, int y) /*008*/ { /*009*/ int t; /*010*/ /*011*/ t = x; /*012*/ x = y; /*013*/ y = t; /*014*/ } /*015*/ SQ Lab. 15
(예) 값 전달 방식(call by value) /*016*/ void swap 02 address(int *x, int *y) /*017*/ { /*018*/ int t; /*019*/ /*020*/ t = *x; /*021*/ *x = *y; /*022*/ *y = t; /*023*/ } /*024*/ SQ Lab. 16
/*025*/ void main() /*026*/ { /*027*/ int a; /*028*/ int b; /*029*/ /*030*/ a = 3; b = 7; /*031*/ cout << "₩n swap before : a=>" << a << " b=>" << b; /*032*/ swap 01 value(a, b); /*033*/ cout << "₩n swap after : a=>" << a << " b=>" << b << "₩n"; /*034*/ /*035*/ a = 3; b = 7; /*036*/ cout << "₩n swap before : a=>" << a << " b=>" << b; /*037*/ swap 02 address(&a, &b); /*038*/ cout << "₩n swap after : a=>" << a << " b=>" << b << "₩n"; /*039*/ /*040*/ cout << "₩n₩n Type any character : "; /*041*/ cin >> a; /*042*/ } SQ Lab. 17
/*027*/ /*028*/ /*030*/ /*032*/ int a; int b; a = 3; b = 7; a(100) b(200) 3 7 swap 01 value(a, b); main() swap 01 value() a(100) b(200) x(800) y(900) 3 7 7 3 /*037*/ swap 02 address(&a, &b); main() SQ Lab. swap 02 address() a(100) b(200) x(800) y(900) 3 7 (100) (200) 7 3 18
[결 과] SQ Lab. 19
(예) 주소 전달 방식(call by reference) /*001*/ // function 11 c. Ncpp /*002*/ /*003*/ #include "iostream" /*004*/ /*005*/ using namespace std; /*006*/ /*007*/ void swap 03 reference(int &x, int &y) /*008*/ { /*009*/ int t; /*010*/ /*011*/ t = x; /*012*/ x = y; /*013*/ y = t; /*014*/ } /*015*/ SQ Lab. 21
/*016*/ void main() /*017*/ { /*018*/ int a; /*019*/ int b; /*020*/ /*021*/ a = 3; b = 7; /*022*/ cout << "₩n swap before : a=>" << a << " b=>" << b; /*023*/ swap 03 reference(a, b); /*024*/ cout << "₩n swap after : a=>" << a << " b=>" << b; /*025*/ /*026*/ cout << "₩n₩n Type any character : "; /*027*/ cin >> a; /*028*/ } SQ Lab. 22
/*018*/ /*019*/ /*021*/ int a; int b; a = 3; b = 7; a (100) /*023*/ b 3 (200) 7 swap 03 reference(a, b); main() a (100) 3 b (200) 7 7 3 x y swap 03 reference() SQ Lab. 23
[결 과] SQ Lab. 24
(예) 디폴트 파라미터의 설정 (1) void function 1(int=100, int=200, float=123. 456, char='A'); (2) void function 2(int, int=200, float=123. 456, char='B'); (3) void function 3(int, float, char='C'); (4) void function 4(int=100, int, float, char); (5) void function 5(int, int=200, float=123. 456, char); (6) void function 6(int=100, int=200, float=123. 456, char); SQ Lab. 26
(예) 디폴트 파라미터 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ /*018*/ /*019*/ SQ Lab. // function 20 c. Ncpp : default parameter #include "iostream" using namespace std; int sum 01(int i=1, int u=1000, int g=1) { int l, total = 0; for(l=i; l<=u; l=l+g) total = total + l; return total; } void main() { int sum 02(int=1, int=1000, int=1); int total; 27
/*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ /*038*/ /*039*/ /*040*/ /*041*/ /*042*/ SQ Lab. total = sum 01(1000, 3000, 2); cout << "₩n sum(1000, 3000, 2) => " << total; total = sum 01(1000, 3000); cout << "₩n₩n sum(1000, 10000) ===> " << total; total = sum 02(1000); cout << "₩n₩n sum(1000) =====> " << total; total = sum 02(); cout << "₩n₩n sum() =======> " << total; cout << "₩n₩n₩n Type any character : "; cin >> total; } int sum 02(int i, int u, int g) { int l; int total = 0; for(l=i; l<=u; l=l+g) total = total + l; return(total); } 28
/*020*/ total = sum 01(1000, 3000, 2); /*007*/ int sum 01(int i=1, int u=1000, int g=1) /*023*/ total = sum 01(1000, 3000); /*007*/ int sum 01(int i=1, int u=1000, int g=1) SQ Lab. 29
/*024*/ total = sum 02(1000); /*007*/ int sum 02(int i=1, int u=1000, int g=1) /*027*/ total = sum 02(); /*005*/ int sum 02(int i=1, int u=1000, int g=1) SQ Lab. 30
[결과] SQ Lab. 31
(예) 지역 변수와 광역 변수 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ /*018*/ /*019*/ /*020*/ /*021*/ SQ Lab. // function 30 c. Ncpp : local & global variable #include "iostream" using namespace std; int g = 10; void function 01() { int x; x=20; g = g + x; cout << "₩n₩n₩n x=>" << x << " g=>" << g; } void main() { int y; y = 30; 34
/*042*/ function 01(); /*043*/ /*044*/ // t = t + f; (error) /*045*/ /*046*/ cout << "₩n₩n₩n Type any character<Enter> : "; /*047*/ cin >> g; /*048*/ } SQ Lab. 36
[결 과] SQ Lab. 37
(예) 지역 함수와 광역 함수 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ /*018*/ /*019*/ /*020*/ /*021*/ SQ Lab. // function 31 c. Ncpp : local function & global function #include "iostream" using namespace std; void function 01() { cout << "₩n function 01 "; } void function 02() { cout << "₩n₩n function 02 "; function 01(); } void main() { void function 03(); 39
/*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ SQ Lab. function 01(); function 02(); function 03(); cout << "₩n₩n₩n Type any character : "; char x; cin >> x; } void function 03() { cout << "₩n₩n function 03 "; }; 40
[결 과] SQ Lab. 41
순환 함수 (recursive function/procedure) 자기 자신을 호출할 수 있는 함수 SQ Lab. 42
(예) factorial : n! = n x (n-1) x (n-2). . . x 3 x 2 x 1 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ SQ Lab. // function 40 c. Ncpp : recursive function #include "iostream" using namespace std; int factorial(int n) { int value; if(n==1) return 1; value = n * factorial(n - 1); return value; } 43
(예) factorial : n! = n x (n-1) x (n-2). . . x 3 x 2 x 1 /*018*/ void main() /*019*/ { /*020*/ int n, return. Value; /*021*/ /*022*/ cout << "₩n Type n : "; /*023*/ cin >> n; /*024*/ /*025*/ if(n>0) /*026*/ { /*027*/ return. Value = factorial(n); /*028*/ cout << "₩n " << n << "! => " << return. Value; /*029*/ } /*030*/ /*031*/ cout << "₩n₩n₩n Type any character<Enter> : "; /*032*/ cin >> n; /*033*/ } SQ Lab. 44
/*027*/ return. Value = factorial(n); SQ Lab. 45
[결 과] SQ Lab. 46
(예) 광역으로만 함수가 중복된 경우 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ /*018*/ /*019*/ /*020*/ /*021*/ SQ Lab. // function 50 c. Ncpp : function overloading (only global) #include "iostream" using namespace std; int add(int x); int add(int x, int y); float add(float x, float y, float z); void main() { int i 1=100; int. Value; float f 1=12. 34, f 2, f 3; float. Value; int. Value = add(i 1); cout << "₩n 1: " << int. Value; 49
/*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ /*038*/ /*039*/ /*040*/ /*041*/ /*042*/ /*043*/ /*044*/ /*045*/ SQ Lab. int. Value = add(i 1, 23); cout << "₩n 2: " << int. Value; float. Value = add(f 1, 13. 57, 24. 68); cout << "₩n 3: " << float. Value; cout << "₩n₩n₩n Type any character : "; cin >> i 1; } int add(int x) { return (x + 11); } int add(int x, int y) { return (x + y); } float add(float x, float y, float z) { return (x + y + z); } 50
[결 과] SQ Lab. 51
(예) 지역으로만 함수가 중복된 경우 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ /*018*/ /*019*/ /*020*/ /*021*/ SQ Lab. // function 51 c. Ncpp : function overloading (only local) #include "iostream" using namespace std; void main() { int add(int x); int add(int x, int y); float add(float x, float y, float z); int i 1=100; int. Value; float f 1=12. 34, f 2, f 3; float. Value; int. Value = add(i 1); cout << "₩n 1: " << int. Value; 52
/*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ /*038*/ /*039*/ /*040*/ /*041*/ /*042*/ /*043*/ /*044*/ /*045*/ SQ Lab. int. Value = add(i 1, 23); cout << "₩n 2: " << int. Value; float. Value = add(f 1, 13. 57, 24. 68); cout << "₩n 3: " << float. Value; cout << "₩n₩n₩n Type any character : "; cin >> i 1; } int add(int x) { return (x + 11); } int add(int x, int y) { return (x + y); } float add(float x, float y, float z) { return (x + y + z); } 53
[결 과] SQ Lab. 54
(예) 광역과 지역에서 함께 함수가 중복된 경우 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ /*018*/ /*019*/ /*020*/ /*021*/ /*022*/ SQ Lab. // function 52 c. Ncpp : function overloading (global & local) #include "iostream" using namespace std; float add(float x, float y, float z); void main() { int add(int x); int add(int x, int y); int i 1=100; int. Value; float f 1=12. 34, f 2, f 3; float. Value; int. Value = add(i 1); cout << "₩n 1: " << int. Value; 56
/*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ /*038*/ /*039*/ /*040*/ /*041*/ /*042*/ /*043*/ /*044*/ /*045*/ /*046*/ SQ Lab. int. Value = add(i 1, 23); cout << "₩n 2: " << int. Value; // float. Value = add(f 1, 13. 57, 24. 68); // cout << "₩n 3: " << float. Value; cout << "₩n₩n₩n Type any character : "; cin >> i 1; } int add(int x) { return (x + 11); } int add(int x, int y) { return (x + y); } float add(float x, float y, float z) { return (x + y + z); } 57
[결 과] SQ Lab. 58
(예) 디폴트 파라미터가 설정되어 선택이 모호할 경우 /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ /*016*/ /*017*/ /*018*/ /*019*/ /*020*/ /*021*/ SQ Lab. // function 53 c. Ncpp : function overloading error #include "iostream" using namespace std; int add(int x); int add(int x, int y=100, int z=1); void main() { int return. Value; return. Value = add(2, 1000, 2); cout << "₩n 1: " << return. Value; return. Value = add(1, 1000); cout << "₩n 2: " << return. Value; // return. Value = add(100); // (error : ambiguous call to overloaded function) 60
/*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ /*035*/ /*036*/ /*037*/ SQ Lab. cout << "₩n₩n₩n Type any character : "; cin >> return. Value; } int add(int x) { return (x + 23); } int add(int x, int y, int z) { int i, total=0; for(i=x; i<=y; i=i+z) total = total + i; return total; } 61
[실습문제] [실습문제 1] call by value and call by reference 다음 프로그램을 수행한 후 화면의 출력 결과를 기술하시오. /*001*/ /*002*/ /*003*/ /*004*/ /*005*/ /*006*/ /*007*/ /*008*/ /*009*/ /*010*/ /*011*/ /*012*/ /*013*/ /*014*/ /*015*/ SQ Lab. // practice 0401 c. Ncpp #include "iostream" using namespace std; void plus 100(int x) { x = x + 100; } void add 100(int *x) { *x = *x + 100; } 62
/*016*/ /*017*/ /*018*/ /*019*/ /*020*/ /*021*/ /*022*/ /*023*/ /*024*/ /*025*/ /*026*/ /*027*/ /*028*/ /*029*/ /*030*/ /*031*/ /*032*/ /*033*/ /*034*/ SQ Lab. void add 500(int &x) { x = x + 500; } void main() { int a=100; plus 100(a); cout << "₩n₩n a=> " << a; add 100(&a); cout << "₩n₩n a=> " << a; add 500(a); cout << "₩n₩n a=> " << a; cout << "₩n₩n₩n Type any character : "; cin >> a; } 63
- Slides: 65