/ W3SCHOOLS

W3schools - Python_Function

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

Function

Is a block of code which only runs when it is called

Can pass data, known as parameters, into a func

A func can return data as a result

Can send any data types of argument to a func, and it will be treated as the same data type inside the func

# Create func using the def keyword
def func():
print(Hello)

# To call a func, use the func name followed by parenthesis
func()	# output “Hello”

Argument

Information can be passed into funcs as arguments

Are specified after the func name, inside the parentheses

Can add as many args as you want, just separate them with a comma

  • Parameter is the var listed inside the parentheses in the func definition

  • Arg is the value that is sent to the func when it is called

By default, a func must be called with the correct number of args

Arbitrary args

If don’t know how many args that will be passed into the func, add a * before the parameter name in the func definition

This way the func will receive a tuple of args, and can access the items accordingly

def func(*kids):
print(The youngest child is  + kids[2])
func(Tom , Emil, Linus)		# output “Linus”

Keyword args

Can also send args with the key = value syntax

This way the order of the args doesn’t matter

def func(kid1, kid2, kid3):
print(The youngest child is  + kid3)
func(kid1 = Tom , kid3 = Linus, kid2 = Emil)		# output “Linus”

Arbitrary kwargs

If don’t know how many kwargs that will be passed into the func, add two asterisk ** before the parameter name in the func definition

This way the func will receive a dict of args, and can access the items accordingly

def func(**kid):
print(His last name is  + kid[lname])
func(fname = Sponge, lname= Bob)	# output “Bob”

# Can use a default parameter value
def func(country = bikini city)
return I am from  + country		# Can return value(s), just separate them with a comma
print(func())	# output “I am from bikini city”

Recursion

Python also accepts func recursion, which means a defined func can call itself

It is a common mathematical and programming concept

This has the benefit of meaning that you can loop through data to reach a result

Should be very careful with recursion as it can be quite easy to slip into writing a func which never terminates, or one that uses excess amounts of memory or processor power

However, when written correctly recursion can be a very efficient and mathematically-elegant approach to programming

def func(k):
if(k>0):
result = k + func(k-1)
else:
result = 0
return result
print(func(5))		# output 15

Lambda

Is a small anonymous func

Can take any number of arguments, but can only have one expression

lambda arguments : expression

The power of lambda is better shown when you use them as an anonymous func inside another func

def func(n):
return lambda a : a * n
tripler = func(3)
doulber = func(2)

print(doubler(11))	# output 22
print(tripler(11))	# output 33