함수란 무엇인가?
함수 만들기
Python
1 2 3 | def a3(): print ( 'aaa' ) a3() |
Ruby
1 2 3 4 | def a3() puts( 'aaa' ) end a3() |
실행결과
1 | aaa |
리턴값
Python
1 2 3 | def a3(): return 'aaa' print (a3()) |
Ruby
1 2 3 4 | def a3() return 'aaa' end puts(a3()) |
실행결과
1 | aaa |
입력값
Python
1 2 3 | def a(num): return 'a' * num print (a( 3 )) |
Ruby
1 2 3 4 | def a(num) return 'a' *num end puts(a( 3 )) |
실행결과
1 | aaa |
여러개의 입력값
Python
1 2 3 | def make_string( str , num): return str * num print (make_string( 'b' , 3 )) |
Ruby
1 2 3 4 | def make_string(str, num) return str*num end puts(make_string( 'b' , 3 )) |
실행결과
1 | aaa |
로그인 애플리케이션 (루비)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | puts( "아이디를 입력해주세요" ) input_id = gets.chomp() def login(_id) members = [ 'egoing' , 'k8805' , 'leezche' ] for member in members do if member == _id return true end end return false end if login(input_id) puts( 'Hello, ' +input_id) else puts( 'Who are you?' ) end |
로그인 애플리케이션 (파이썬)
1 2 3 4 5 6 7 8 9 10 11 | input_id = input ( "아이디를 입력해주세요.\n" ) def login(_id): members = [ 'egoing' , 'k8805' , 'leezche' ] for member in members: if member = = _id: return True return False if login(input_id): print ( 'Hello, ' + input_id) else : print ( 'Who are you?' ) |