T. 김동식
Java Script
함수
// 1 document.write를 하다
function f1() {document.write("f1", "<br>");}
f1();f1();
// 2 document.write에 변수를 넣다
function f2(x) {document.write(x);}
f2("f2"+'<br>');
// 3 return을 하다
function f3() {return "f3";}
let ff3 = f3();
document.write(f3(), '<br>', ff3, '<br>')
// 4 return에 변수를 넣다.
function f4(x) {return x;}
let ff4 = f4("f4");
document.write(ff4+'<br>', f4("f4")+'<br>');
익명함수
let add2 = () =>{return "add2"}
document.write('<br>2. ',add2())
템플릿 리터럴 : 백틱` 달러 ${} + 기본값세팅
function print1(name, count){
// 유효성검사
if(typeof(count) == 'undefined'){
count ="(기본값)"
}
document.write(`${name}와 ${count}이/가 있습니다.`);
}
// print1();
// print1("a", "b");
print1("a");
function print2(name, count=1){
// // 유효성검사
// if(typeof(count) == 'undefined'){
// count ="(기본값)"
// }
document.write(`${name}와 ${count}이/가 있습니다.`);
}
// print2();
// print2("a", "b");
document.write('<br><br>');
print2("a");
callback 함수
called at the back
function callTenTimes(func) {
for (let i = 0; i < 10; i++) {
func();
}
}
callTenTimes(function () {
document.write("호출했다.", '<br>');
});
Python
익명함수 = function lambda
>>> lambda a,b : a+b
<function <lambda> at 0x0000023A8454F0D0>
>>> add=lambda a,b : a+b
>>> type(add)
<class 'function'>
>>> add(3,4)
7
>>> result = add(3,4)
>>> result
7