πŸ“κ°œλ°œ 일지 (8) - 2

우민·2023λ…„ 9μ›” 11일
0

개발 일지

λͺ©λ‘ 보기
9/9

바삐 νƒœμ–΄λ‚˜μ§€ μ•Šμ€ μžλŠ” 바삐 μ£½λŠ”λ‹€.
" He not busy being born is busy dying. "
- λ°₯ λ”œλŸ°

μ§€κΈˆ ν•  일을 계속 미루닀 보면,
μΈμƒμ˜ μ„±κ³΅μœΌλ‘œ κ°€λŠ” 길도 점점 λ©€μ–΄μ§„λ‹€. 

πŸ“Œ Day - 9

[ Function ]

1. λͺ¨λ“ˆν™” ( Modularization )

  • 각 ν•¨μˆ˜μ— 역할을 λΆ€μ—¬ν•˜μ—¬ μ†Œν”„νŠΈμ›¨μ–΄ 개발과 μœ μ§€λ³΄μˆ˜λ₯Ό νŽΈλ¦¬ν•˜κ²Œ ν•΄μ€Œ
μ˜ˆμ‹œ)

#include <iostream>


// 두 수λ₯Ό λ”ν•˜λŠ” ν•¨μˆ˜
int add(int a, int b) 
{
    return a + b;
}


// 두 수λ₯Ό κ³±ν•˜λŠ” ν•¨μˆ˜
int multiply(int a, int b) 
{
    return a * b;
}


int main() 
{
    // λ§μ…ˆ ν•¨μˆ˜ 호좜
    int sum = add(num1, num2);
    std::cout << "두 수의 ν•©: " << sum << std::endl;

    // κ³±μ…ˆ ν•¨μˆ˜ 호좜
    int product = multiply(num1, num2);
    std::cout << "두 수의 κ³±: " << product << std::endl;

    return 0;
}

2. ν•¨μˆ˜ 이점 ( Advantage )

"독립성 :"
각 ν•¨μˆ˜ λ˜λŠ” λͺ¨λ“ˆμ€ νŠΉμ • μž‘μ—…μ„ μˆ˜ν–‰ν•˜κΈ° μœ„ν•΄ μ„€κ³„λ˜μ—ˆμœΌλ©°, 
λ‹€λ₯Έ λͺ¨λ“ˆκ³Ό λ…λ¦½μ μœΌλ‘œ μž‘λ™ν•¨


"μž¬μ‚¬μš©μ„± :"
ν•¨μˆ˜ λͺ¨λ“ˆν™”λŠ” μ½”λ“œμ˜ μž¬μ‚¬μš©μ„ 촉진함 λ™μΌν•œ κΈ°λŠ₯이 μ—¬λŸ¬ κ³³μ—μ„œ ν•„μš”ν•œ 경우 
ν•΄λ‹Ή ν•¨μˆ˜ λ˜λŠ” λͺ¨λ“ˆμ„ μ—¬λŸ¬ κ³³μ—μ„œ ν˜ΈμΆœν•˜μ—¬ 쀑볡 μ½”λ“œλ₯Ό 피함


"디버깅 및 ν…ŒμŠ€νŠΈ μš©μ΄μ„± :"
μž‘μ€ λͺ¨λ“ˆ λ‹¨μœ„λ‘œ μ½”λ“œλ₯Ό λΆ„ν• ν•˜λ©΄ 각 λͺ¨λ“ˆμ„ 더 μ‰½κ²Œ 
λ””λ²„κ·Έν•˜κ³  ν…ŒμŠ€νŠΈν• μˆ˜ 있으며 버그λ₯Ό μ°Ύμ•„λ‚΄κ³  μˆ˜μ •ν•˜κΈ°κ°€ 더 μš©μ΄ν•¨


"μœ μ§€ 보수 μš©μ΄μ„± :"
μ½”λ“œ 변경이 ν•„μš”ν•œ 경우, κ΄€λ ¨ λͺ¨λ“ˆλ§Œ μˆ˜μ •ν•˜λ©΄ λ˜λ―€λ‘œ
μœ μ§€ λ³΄μˆ˜κ°€ 간단해지며 λ‹€λ₯Έ 뢀뢄에 영ν–₯을 λ―ΈμΉ˜μ§€ μ•Šκ³  λ³€κ²½ κ°€λŠ₯함


[ Factorial ]

1. ν˜•μ‹ ( Form )

n = n * (n - 1) * (n - 2) * ㆍㆍㆍ * 1

2. For 반볡문

// Factorial λͺ¨λ“ˆν™”
int Factorial(int iCount)
{
	int iValue = 1;

	for (int j = 1; j < iCount; ++j)
	{
		iValue *= (j + 1);
	}
	

	return iValue;
}

int main()
{	
	int iValue = Factorial(5);
	
	
	return 0;

} 

3. While 반볡문

// Factorial λͺ¨λ“ˆν™”
int Factorial(int iCount)
{
	int iValue = 1;
	int j = 1;

	while (j < iCount)
	{
		iValue *= (j + 1);
		j += 1;
	}


	return iValue;
}


int main()
{
	int iValue = Factorial(5);
    
    
    return 0;
    
}

[ Pibonacci Numbers ]

1. ν˜•μ‹ ( Form )

0 + 1 = 1,	1 + 1 = 2,	1 + 2 = 3,	2 + 3 = 5,	3 + 5 = 8,	5 + 8 = 13

ㆍㆍㆍ

2. For 반볡문

// Pibonacci λͺ¨λ“ˆν™”
int Fibonacci(int pNum)
{
	int num1 = 1;
	int num2 = 1;
	int iValue = 0;

	if (pNum == 1 || pNum == 2)
	{
		return 1;
	}

	else
	{
		for (int i = 0; i < pNum - 2; ++i)
		{
			iValue = num1 + num2;
			num1 = num2;
			num2 = iValue;
		}

		return iValue;
	
    
    
int main()
{
	iValue = Fibonacci(10);	
	
	
	return 0;

}

3. While 반볡문

// Pibonacci λͺ¨λ“ˆν™”
int Fibonacci(int pNum)
{
	int num1 = 1;
	int num2 = 1;
	int iValue = 0;
    int i = 1;

	if (pNum == 1 || pNum == 2)
	{
		return 1;
	}

	else
	{
    	while (i < pNum - 2)
        {
        	iValue = num1 + num2;
            num1 = num2;
            num2 = iValue;
        }
   	}

		return iValue;
	
    
    
int main()
{
	iValue = Fibonacci(5);	
	
	
	return 0;

}

πŸ’¬ COMMENT

λ³΄μ‹œλŠ” 뢄이 λ§Žμ§€λŠ” μ•Šκ² μ§€λ§Œ ν˜Ήμ‹œ 잘λͺ»λœ 정보가 있으면 μ•Œλ €μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€

profile
ν•™μŠ΅ν•˜λŠ” 쀑

0개의 λŒ“κΈ€