[프로그래밍 언어] 2. Names, Bindings, and Scopes
Y
Yerim
- Programming Language
x = [2, 3, 4]
y = x # 참조가 복사
y[2] = 15 # [2, 3, 15]?
print(x) # x = [2, 3, 15]
print(y) # y = [2, 3, 15]i = 0; // 10을 i라는 변수 주소에
j = i; // i의 값을 j의 주소에int* a;
a = 100;
*a = 10;char c;
c = 7; // dtype에 따라 해석이 달라진다typedef int INT; // int: 데이터 타입
typedef struct A x; // struct A: 저장할 수 있는 값int i; // 1) 컴파일 시점
i = 10; // 2) 실행 시간
// 1) float f
// 2) f = 5
float f = 5;int i = 10;
var j = 10;spam = 'Hello' # 문자열 타입
spam = 40 # 정수 타입
spam = 1.0 # 실수 타입// Java
Integer intnode;
intnode = new Integer(1); // new: 명시적인 동적변수 할당, 1을 초기값으로 사용
// 회수 명령어 없음 -> dangling pointer 문제 해결// C언어
int *intnode;
// intnode: 전역 vs 지역에 따라 다름
// (int *)malloc(sizeof(int)): 명시적인 동적 변수
intnode = (int *)malloc(sizeof(int));
free(intnode);// C++
int *intnode;
intnode = new int; // new: 명시적인 동적변수 할당
...
delete intnode; // delete: 명시적인 동적 변수 회수# python
highs = [74, 84, 86, 90, 71]
highs = 17.3// JavaScript
highs = [74, 84, 86, 90, 71];function big(){
functions sub1(){
var x = 7;
sub2();
}
function sub2(){
var y = x;
}
var x = 3;
sub1();
}if(list[i] < list[j]){
int temp;
temp = list[i];
...
}void sub(){
int count;
...
while(...){
int count;
count++;
...
}
}void func(){
...
for(int count=0; count<10; count++){
...
}
}day = "Monday"
def tester():
print("The global day is ", day)
tester() # 정상적 출력: The global day is Mondayday = "Monday"
def tester():
print("The global day is: ", day)
day = "Tuesday"
print("The new value of day is: ", day)
tester() # UnboundLocalError오류 -> tester의 두 번째 day는 지역변수day = "Monday"
def tester():
global day
print("The global day is: ", day)
day = "Tuesday"
print("The new value of day is: ", day)
tester() # 정상적 출력function big(){
function sub1(){
var x = 7;
}
functions sub2(){
var Y = x;
var z = 3;
}
var x = 3;
}void printheader(){
...
}
void compute(){
int sum;
...
printheader();
}g = 3
def sub1():
a = 5
b = 7
... # 프로그램 지점 1
def sub2():
global g
c = 9
... # 프로그램 지점 2
def sub3():
nonlocal c
g = 11
... # 프로그램 지점 3void sub1(){
int a, b;
... // 프로그램 지점 1
}
void sub2(){
int b, c;
... // 프로그램 지점 2
sub1();
}
void main(){
int c, d;
... // 프로그램 지점 3
sub2();
}