Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer...
Transcript of Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer...
![Page 1: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/1.jpg)
Human-Computer Interaction Laboratory @ SNU
Functions
storage class, parameter passing, scope rules, recursion
![Page 2: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/2.jpg)
Human-Computer Interaction Laboratory @ SNU
Getting Multiple Results from a Function
1-2
![Page 3: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/3.jpg)
Human-Computer Interaction Laboratory @ SNU
Diagram of Function separate with Multiple Results
![Page 4: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/4.jpg)
1-4
Function Call
![Page 5: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/5.jpg)
1-5
![Page 6: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/6.jpg)
Human-Computer Interaction Laboratory @ SNU
Declaration and Definition
• Declaration of a function prototype
– return type
– function name
– parameters
• Definition of a function
![Page 7: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/7.jpg)
#include <graphics.h>#include <conio.h>void five_circle(int x, int y); /* declaration */int main(void){
int n, m, l;
….for(n = 1; n <= 5; n++)
for(m = 1; m <= 5; m++)five_circle(50*n, 50*m); /* function call */
….
return 0;}
void five_circle(int x, int y) /* header */ /* function definition */
{
/* body starts here */
int l;
for(l = 1; l <= 5; l++) circle(x, y, 5*l);
}
![Page 8: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/8.jpg)
Human-Computer Interaction Laboratory @ SNU DrawSquare (running) called DrawLine
Call Stack (run-time stack)
• stores information about the active subroutines of a
computer program (where to return after finishing exec)
• stack frames, activation frames, or activation records
• Execution stack
• Control stack
• Run-time stack
• Machine stack
• The Stack
![Page 9: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/9.jpg)
Human-Computer Interaction Laboratory @ SNU
Call-by-value
• C
![Page 10: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/10.jpg)
#include <stdio.h>void swap(int, int);int main(void){
int a=1,b=2;printf("Before swap : a=%d, b=%d\n",a,b);swap(a,b);printf("After swap : a=%d, b=%d\n",a,b);return 0;
}
void swap(int a, int b){
int temp;temp=a;a=b;b=temp;
}
![Page 11: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/11.jpg)
![Page 12: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/12.jpg)
![Page 13: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/13.jpg)
![Page 14: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/14.jpg)
Human-Computer Interaction Laboratory @ SNU
Scope Rules
•
• (
.)
![Page 15: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/15.jpg)
#include <stdio.h>int main(void){
int a = 0, b = 1;
printf("1st a: %d, b: %d\n", a, b);{
int a = 3;printf("2nd a: %d, b: %d\n", a, b);
}printf("1st a: %d, b: %d\n", a, b);return 0;
}
![Page 16: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/16.jpg)
{int a=1, b=2, c=3;printf("%3d%3d%3d\n", a, b, c); /* 1 2 3 */
{int b=4;float c=5.0;printf("%3d%3d%5.1f\n", a, b, c); /* 1 4 5.0 */a = b;
{int c; c = b;printf("%3d%3d%3d\n", a, b, c); /* 4 4 4 */
} printf("%3d%3d%5.1f\n", a, b, c); /* 4 4 5.0 */
} printf("%3d%3d%3d\n", a, b, c); /* 4 2 3 */
}
![Page 17: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/17.jpg)
{int a, b;.....{ /* inner block 1 */
float b;..... /* int a is known, but not int b */
}.....{ /* inner block 2 */
float a;..... /* int b is known, but not int a *//* nothing in inner block 1 is known */
}....
}
![Page 18: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/18.jpg)
#include <stdio.h>
int a();int b();int c();
int a(){
b();c();return 0;
}
int b(){ return 0; }
int c(){ return 0; }
int main(){
a();return 0;
}http://www.tenouk.com/ModuleZ.html
Call Stack – Example
![Page 19: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/19.jpg)
Human-Computer Interaction Laboratory @ SNU
Process Address Space
code segment
![Page 20: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/20.jpg)
Human-Computer Interaction Laboratory @ SNU
Block Structure
• 메모리 절약 : 메모리의 지역적 할당(필요한 곳에서만 할당)
• 함수정의는 모두 병렬 블록 구조
• 오류 수정(debugging)을 위해 임시로 블록을 삽입하여 작업
– 다른 부분에 영향을 주지 않고 작업 후 삭제
![Page 21: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/21.jpg)
Human-Computer Interaction Laboratory @ SNU
Storage Class
• 변수의 세가지 속성
– name
– type
– storage class
• automatic, external, register, static
• scope (visibility)
![Page 22: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/22.jpg)
Human-Computer Interaction Laboratory @ SNU
Automatic
• 기억영역 클래스를 지정하지 않고 블록 내에서 변수를 선언하면 모두 자동기억 클래스로 지정된다.
→ auto 키워드는 대부분 생략함
• 유효 범위 : 선언된 이후부터 선언된 블록의 끝까지
• 생존 기간 : 블록 진입시 생성/블록 탈출시 소멸
• 기억 장소 : stack 영역에 저장
![Page 23: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/23.jpg)
Human-Computer Interaction Laboratory @ SNU
Register
• 빠른 수행시간이 요구되는 경우 사용된다.
• 유효 범위 : 선언된 이후부터 선언된 블록의 끝까지
• 생존 기간 : 블록 진입시 생성/블록 탈출시 소멸
• 기억 장소 : CPU내의 register에 저장
• 컴파일러가 쓸 수 있는 register의 수가 제한되어 있음
• register로 선언된 변수가 너무 많으면 stack에 저장됨
![Page 24: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/24.jpg)
Human-Computer Interaction Laboratory @ SNU
Static ( )
• 다음에 블록을 들어갈 때 예전 값을 그대로 유지하고 싶은 경우 사용됨
• 유효 범위 : 선언된 이후부터 선언된 블록의 끝까지• 생존 기간 : 프로그램 시작시 생성/프로그램 종료시 소멸• 기억 장소 : 정적 영역에 저장
void f(void){
static int cnt = 0;
++cnt;if(cnt%2 == 0)..... /* do something */else..... /* do something different */
}
![Page 25: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/25.jpg)
Human-Computer Interaction Laboratory @ SNU
External
• 블록과 함수간에 정보를 교환하기 위한 한 방법으로 사용됨
• 유효 범위 : 선언된 이후부터 선언된 파일의 끝까지
• 생존 기간 : 프로그램 시작시 생성/프로그램 종료시 소멸
• 기억 장소 : 정적 영역에 저장
![Page 26: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/26.jpg)
Human-Computer Interaction Laboratory @ SNU
http://en.wikipedia.org/wiki/External_variable
External Variable
• Definition and DeclarationFile 1:
int GlobalVariable=1; /* implicit definition */
void SomeFunction(void); /* function prototype (declaration) */
int main() {
GlobalVariable = 2;
SomeFunction();
return 0;
}
File 2:
extern int GlobalVariable; /* explicit declaration */
void SomeFunction(void) { /* function header (definition) */
++GlobalVariable;
}
![Page 27: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/27.jpg)
Human-Computer Interaction Laboratory @ SNU
Using Header Files
file2.h:
extern int GlobalVariable; /* explicit declaration */
file1.c:
#include “file2.h” /* Declaration made available here */
int GlobalVariable=1; /* implicit definition */
void SomeFunction(void); /* function prototype (declaration) */
int main() {
GlobalVariable = 2;
SomeFunction();
return 0;
}
file2.c:
#include “file2.h” /* Declaration made available here */
void SomeFunction(void) { /* function header (definition) */
++GlobalVariable;
}
![Page 28: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/28.jpg)
Human-Computer Interaction Laboratory @ SNU
int a; /* can be visible outside this file */static int b; /* only visible throughout this file */
int func(void) {int c; /* lifetime: from beginning to end of this function (block) */static int d; /* lifetime: static -> with the same lifetime as the program */...
}
Static Variable
• static global vs. static local
![Page 29: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/29.jpg)
Human-Computer Interaction Laboratory @ SNU
First Try
int factorial(int n) {
return n * factorial(n - 1); }
int main(void){
int n=5;printf(“%d!=%d”, n, factorial(n));
}
![Page 30: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/30.jpg)
Human-Computer Interaction Laboratory @ SNU
Second Try
int factorial(int n) {
if (n==1) return 1;
elsereturn n * factorial(n - 1);
}
int main(void){
int n=5;printf(“%d!=%d”, n, factorial(n));
}
![Page 31: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/31.jpg)
Human-Computer Interaction Laboratory @ SNU
Recursion: Factorial Example
![Page 32: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/32.jpg)
Human-Computer Interaction Laboratory @ SNU
• 장점
–프로그램을 간결하게 작성할 수 있다.
–이해하기 쉽다.
–유지보수가 쉽다.
• 단점
–메모리가 많이 필요하다.
• 재귀호출이 반복되면 스택영역에 인자와 변수가쌓인다.
–느리다.
![Page 33: Functionsdcslab.snu.ac.kr/courses/pp2020s/Lecture7.pdf · 2020. 5. 4. · Human-Computer Interaction Laboratory @ SNU Functions storage class, parameter passing, scope rules, recursion](https://reader035.fdocuments.net/reader035/viewer/2022071114/5feada4e840c0e02631e0404/html5/thumbnails/33.jpg)
Human-Computer Interaction Laboratory @ SNU
Iterative Version
int factorial(int n)
{ /* iterative version */
int product = -1;
for( ; n>1; --n)
product *= n;
return product;
}