알파카징징이 알파카징징이 코딩하는 알파카

데이터 구조 및 분석 ch_1_8 Function Statement

» writing

KAIST 산업및시스템공학과 문일철_ 데이터 구조 및 분석 수업을 참고하여 작성하였습니다

ch_1_8 Function Statement

정의


Function Statement

1. Function Statement
: y = f(x)
: output = f(input)
: return multiple variable (keep them in order)
: don't hove to specify return type
: One line function is called lambda function
- def name(parameters) :
    statements            -> 함수내용
    return val1, val2 ... -> 결과물
numA = 1
numB = 2

def add(numParam1, numParam2) :
    return numParam1 + numParam2
def multiply(numParam1, numParam2) :
    return numParam1*2, numParam2*3
def increase (numParam1, step = 1) :
    return numParam1 + step

numC = add(numA, numB)
numD, numE = multiply(numA, numB)
numF = increase(numA, 5)
numG = increase(numA)

lambdaAdd = lambda numParam1, numParam2 : numParam1 + numParam2
numH = lambdaAdd(numA, numB)
print(numC, numD, numE,numF, numG, numH)

# 3 2 6 6 2 3
2. Example Program : Finding Prime Numbers (소수 출력하기)
def isPrimeNumber(numParam1) :
    for itr in range(2, numParam1):
        if numParam1 % itr == 0:
            break
    else :
        return True
    return False
# --> Function for calculation

def findPrimes(numParam1, numParam2):
    numCount = 1
    for itr in range(numParam1, numParam2):
        if isPrimeNumber(itr) == True :
            print(numCount, "th prime : ", itr)
            numCount = numCount + 1
# --> Function for iteration
findPrimes(1,10)      # --> Function call

# 1 th prime :  1
# 2 th prime :  2
# 3 th prime :  3
# 4 th prime :  5
# 5 th prime :  7