๋ฉ์๋ : ๊ฐ์ฒด์ ์ผ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ ๋๋ ๋ฐฉ์
C#์์๋ ๋ฌด์กฐ๊ฑด ํด๋์ค ๋ด๋ถ์ ์ ์ธํ๋ค.
๋ฉ์๋๋ ์ฝ๋๋ฅผ ๊ฐ์ถ๋ฆฌ๋ ํํธ, ์ฝ๊ธฐ ์ฝ๊ณ ์ดํดํ๊ธฐ ์ฝ๊ฒ ๋ง๋ ๋ค.
๊ฐ๋ น ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ,
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static void Main(string[] args)
{
int a, b;
a = 3;
b = 4;
WriteLine("Input : {0},{1}", a, b); //Input : 3,4
int x = a + b;
a = 5;
b = 6;
WriteLine("Input : {0},{1}",a,b); //Input : 5,6
int y = a + b;
a = 7;
b = 9;
WriteLine("Input : {0},{1}", a, b); //Input : 7,9
int z = a + b;
}
}
}
์๋์ ๊ฐ์ด ๊ฐ๊ฒฐํํ ์ ์๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
public static int Plus(int a, int b)
{
WriteLine($"Input : {a},{b}");
int result = a+b;
return result;
}
static void Main(string[] args)
{
int x = Plus(3, 4);
int y = Plus(5, 6);
int z = Plus(7, 8);
}
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
Input : 3,4
Input : 5,6
Input : 7,9
๋ฉ์๋๋ ํด๋์ค ์์์ ์ ์ธ๋๋ค.
C#์ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ด๊ณ ,
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์์๋ ์ฝ๋ ๋ด์ ๋ชจ๋ ๊ฒ์ ๊ฐ์ฒด๋ก ํํํ๋ค.
๊ฐ ๊ฐ์ฒด๋ ์์ ๋ง์ ์์ฑ(๋ฐ์ดํฐ)์ ๊ธฐ๋ฅ(๋ฉ์๋)๋ฅผ ๊ฐ์ง๊ณ ์๋๋ฐ,
ํด๋์ค๊ฐ ๋ฐ๋ก ์ด ๊ฐ์ฒด๋ฅผ ์ํ ์ฒญ์ฌ์ง์ ์ ๊ณตํ๋ค.
๊ทธ๋์ ๋ฉ์๋๊ฐ ํด๋์ค ์์์ ์ ์ธ๋๋ ๊ฒ์ด๋ค.
ํํธ, ํด๋์ค ์์ ์ ์ธ๋๋ ๋ฉ์๋๋ ๋งค๊ฐ ๋ณ์์ ๋ฐํ ํ์์ ๊ฐ์ง๋ค.
๋งค๊ฐ ๋ณ์๋ ์ ํ์ ๋ง๋ค๊ธฐ ์ํด ๊ธฐ๊ณ(๋ฉ์๋)์ ์ง์ด ๋ฃ๋ ์ฌ๋ฃ์ด๋ฉฐ,
๊ธฐ๊ณ(๋ฉ์๋)๋ ์ ๋ ฅ๋ ์ฌ๋ฃ(๋งค๊ฐ ๋ณ์)๋ฅผ ๋ฐํ์ผ๋ก ์ ํ์ ๋ง๋ค์ด๋ด๋ฉฐ,
๋ฉ์๋์ ๋ฐํ ํ์์ ์ ํ์ ๊ท๊ฒฉ์ด๋ผ๊ณ ํ ์ ์๋ค.
ํธ์ถ์๊ฐ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์(Argument)๋ฅผ ๋๊ธฐ๋ฉด,
์ด ์ธ์๋ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์(Parameter)์ ์ ๋ ฅ๋๋ค.
ํธ์ถ์ ๋ฐ์ ๋ฉ์๋๋ ๋งค๊ฐ ๋ณ์๋ฅผ ์ด์ฉํ์ฌ ๊ณ์ฐ์ ์ํํ ํ ๊ฒฐ๊ณผ๋ฅผ ํธ์ถ์์๊ฒ ๋ฐํํ๋ค.
<๋งค๊ฐ๋ณ์์ ์ธ์>
๋งค๊ฐ๋ณ์๋ ๋ฉ์๋๊ฐ ํธ์ถ์์๊ฒ์ ์ ๋ฌ๋ฐ์ ๊ฐ์ ๋ฐ๋ ๋ณ์๋ฅผ ๋งํ๊ณ ,
์ธ์๋ ํธ์ถ์๊ฐ ๋งค๊ฐ๋ณ์์ ๋๊ธฐ๋ ๊ฐ์ ๋ปํ๋ค.
<๊ณ์ฐ๊ธฐ ์์ >
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
public static int Plus(int a, int b)
{
return (a + b);
}
public static int Minus(int a, int b)
{
return (a - b);
}
static void Main(string[] args)
{
int result = Plus(3, 4);
WriteLine(result); //7
result = Minus(5,2);
WriteLine(result); //3
}
}
}
<void ๋ฐํ ํ์>
๋ชจ๋ ๋ฉ์๋๋ค์ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ๊ฒ์ ์๋๋ค.
์ด๋ค ๋ฉ์๋๋ค์ ๊ทธ๋ฅ ์๊ธฐ ํ ์ผ๋ง ํ๊ณ ์ข ๋ฃํ๊ธฐ๋ ํ๋ค.
Console ํด๋์ค์ Write()sk WriteLine() ๋ฉ์๋๊ฐ ๊ทธ๋ฐ ์์ด๋ค.
์ด๋ฐ ๋ฉ์๋๋ฅผ ์ ์ธํ ๋๋ ๋ฐํ ํ์์ void๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
Main ๋ฉ์๋ ์ญ์ void ๋ฐํ ํ์์ด๋ค.
<static ํ์ ์>
static์ ์ฌ์ ์ ์ผ๋ก '์ ์ '์ด๋ผ๋ ๋ป์ ๊ฐ์ง๊ณ ์๋ค. ์์ง์ด์ง ์๋๋ค๋ ๋ป์ด๋ค.
C#์์ static์ ๋ฉ์๋๋ ํ๋๊ฐ ํด๋์ค์ ์ธ์คํด์ค๊ฐ ์๋ ํด๋์ค ์์ฒด์ ์์๋๋๋ก ์ง์ ํ๋ ํ์ ์์ด๋ค.
์์ ์์ ์ฝ๋ ์์ Plus()๋ฉ์๋์ Minus() ๋ฉ์๋๋ฅผ static ํ์ ์๋ก ์์ํ์ฌ,
MainApp(ํด๋์ค๋ช )์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ง ์๊ณ ๋ ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.
<return>
return์ ์ ํ๋ฌธ์ ํ ์ข ๋ฅ๋ก, ํ๋ก๊ทธ๋จ์ ํ๋ฆ์ ๊ฐ์๊ธฐ ํธ์ถ์์๊ฒ๋ ๋๋ ค๋๋๋ค.
return ๋ฌธ์ ๋ฉ์๋์ ๋ ๋ฟ๋ง์ด ์๋๋ผ
์ธ์ ๋ ์ง ๋ฉ์๋ ์ค๊ฐ์ ํธ์ถ๋์ด ๋ฉ์๋๋ฅผ ์ข ๊ฒฐ์ํค๊ณ
(๋ฉ์๋ ์ค๊ฐ์ ํธ์ถํ๋ ๊ฒฝ์ฐ ๋ฉ์๋์ ์คํ๋์ง ์์ ์ฝ๋๊ฐ ์๊ธธ ์ ์๋ค.)
ํ๋ก๊ทธ๋จ์ ํ๋ฆ์ ํธ์ถ์์๊ฒ ๋๋ ค์ค ์ ์๋ค.(ํธ์ถ์์๊ฒ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ)
์ฆ, ํ๋ก๊ทธ๋จ์ ์ ์ด๋ฅผ ์ฆ์ ํธ์ถ์์๊ฒ ์ด๋์ํจ๋ค.
๋ฐํ ํ์๊ณผ ์ผ์นํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํด์ผ ํ๋ค.
๋ฉ์๋๊ฐ void ํ์์ธ ๊ฒฝ์ฐ ๋ฐํ๊ฐ ์์ด ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
<return ์์ ํผ๋ณด๋์น>
namespace Hello
{
class MainApp
{
int Fibonacci(int n)
{
if (n < 2)
return n;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
static void Main(string[] args)
{
}
}
}
ํผ๋ณด๋์น ์๋ 0๊ณผ 1๋ก ์์ํ๋ฉฐ, ๋ค์ ํผ๋ณด๋์น ์๋ ๋ฐ๋ก ์์ ๋ ํผ๋ณด๋์น ์์ ํฉ์ด ๋๋ค.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987 (ํผ๋ณด๋์น์)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (์๋ฒ)
ํผ๋ณด๋์น 4์ ๊ฒฝ์ฐ
int Fibonacci(int n)
{
if (n < 2)
return n;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
ํผ๋ณด๋์น 3 + ํผ๋ณด๋์น 2
( 1) + (1 +0)
(1+0)
=3
ํผ๋ณด๋์น์ฐธ๊ณ :[ํผ๋ณด๋์น ์์ด] ํผ๋ณด๋์น ์์ด์ ์ดํด : ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ (naver.com)
[Algorithm] ๋ฒ์์ ๋ฐ๋ฅธ ํผ๋ณด๋์น ์ ๊ตฌํ๋ ๋ฐฉ๋ฒ (tistory.com)
0๋ฒ์งธ: 0
1๋ฒ์งธ: 1
2๋ฒ์งธ: 1
3๋ฒ์งธ: 2
4๋ฒ์งธ: 3
5๋ฒ์งธ: 5
6๋ฒ์งธ: 8
7๋ฒ์งธ: 13
8๋ฒ์งธ: 21
9๋ฒ์งธ: 34
10๋ฒ์งธ: 55
<์ฌ๊ทํธ์ถ>
๋ฉ์๋๊ฐ ์๊ธฐ ์์ ์ ์ค์ค๋ก ํธ์ถํ๋ ๊ฒ์ ์ผ์ปฌ์ด ์ฌ๊ทํธ์ถ์ด๋ผ๊ณ ํ๋ค.
์์ Fibonacci() ๋ฉ์๋๊ฐ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ ๋ค์ Fibonacci() ํจ์ ์์ ์ ํธ์ถํ๋ค.
์ ํ์ ์ธ ์ฌ๊ท ํธ์ถ์ ์์ด๋ค.
์ฌ๊ท ํธ์ถ์ ์ฝ๋๋ฅผ ๋จ์ํ๊ฒ ๊ตฌ์ฑํ ์ ์๋ค๋ ์ฅ์ ์ด ์๋ ํํธ
(์ฌ๊ท ํธ์ถ์ด ์๋ค๋ฉด ์์ ์ฝ๋๋ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๊ตฌ์ฑํด์ผ ํ์ ๊ฒ์ด๋ค.)
์ฑ๋ฅ์์ผ๋ก๋ ๋์ ์ํฅ์ ์ฃผ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์ ์ฃผ์๊ฐ ํ์ํ๋ค.
๋ฉ์๋๊ฐ ๋ฐํํ ๊ฒ์ด ์๋ฌด๊ฒ๋ ์๋ ๊ฒฝ์ฐ,
์ฆ ๋ฐํ ํ์์ด void์ธ ๊ฒฝ์ฐ์๋
return ๋ฌธ์ ์ฌ์ฉํ ์ ์๋ค.(return๋ฌธ์ ๋ง๋๋ฉด ๊ทธ๋ฅ ํ์ถ, ๋น ์ ธ๋๊ฐ!=>
๋ง๋์๋ง์ ๋ฐ๋ก, ํธ์ถ์์๊ฒ ํ๋ก๊ทธ๋จ ํ๋ฆ์ ๋๋ ค์ค๋ค)
<์์ ํ๋ก๊ทธ๋จ>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class MainApp
{
static int Fibonachi(int n)
{
if(n<2)
{
return n;
}
else
{
return Fibonachi(n-1) + Fibonachi(n-2);
}
}
static void PrintProfile(string name,string phone)
{
if(name =="")
{
WriteLine("์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์.");
return;
}
WriteLine($"name : {name}, phone : {phone}");
}
static void Main(string[] args)
{
for(int i=0; i<17; i++)
{
WriteLine($"ํผ๋ณด๋์น {i}์.... {Fibonachi(i)}");
}
PrintProfile("","123-4567");
PrintProfile("๋ฐ์ํ", "456-1230");
}
}
}
<์ ์์ ํ๋ก๊ทธ๋จ ์ถ๋ ฅ ๊ฒฐ๊ณผ>
ํผ๋ณด๋์น 0์.... 0
ํผ๋ณด๋์น 1์.... 1
ํผ๋ณด๋์น 2์.... 1
ํผ๋ณด๋์น 3์.... 2
ํผ๋ณด๋์น 4์.... 3
ํผ๋ณด๋์น 5์.... 5
ํผ๋ณด๋์น 6์.... 8
ํผ๋ณด๋์น 7์.... 13
ํผ๋ณด๋์น 8์.... 21
ํผ๋ณด๋์น 9์.... 34
ํผ๋ณด๋์น 10์.... 55
ํผ๋ณด๋์น 11์.... 89
ํผ๋ณด๋์น 12์.... 144
ํผ๋ณด๋์น 13์.... 233
ํผ๋ณด๋์น 14์.... 377
ํผ๋ณด๋์น 15์.... 610
ํผ๋ณด๋์น 16์.... 987
์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์.
name : ๋ฐ์ํ, phone : 456-1230
<๊ณ์ฐ๊ธฐ ํ๋ก๊ทธ๋จ>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class Calculator
{
public static int Plus(int a, int b)
{
WriteLine($"Input : {a},{b}");
int result = a + b;
return result;
}
public static int Minus(int a, int b)
{
WriteLine($"Input : {a},{b}");
int result = a - b;
return result;
}
}
class MainApp
{
static void Main(string[] args)
{
int result = Calculator.Plus(3,4);
WriteLine(result);
result = Calculator.Minus(5, 2);
WriteLine(result);
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
Input : 3,4
7
Input : 5,2
3
<๋งค๊ฐ๋ณ์>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Hello
{
class Calculator
{
public static int Plus(int a, int b)
{
WriteLine($"Input : {a},{b}");
int result = a + b;
return result;
}
public static int Minus(int a, int b)
{
WriteLine($"Input : {a},{b}");
int result = a - b;
return result;
}
}
class MainApp
{
static void Main(string[] args)
{
int x = 3;
int y = 4;
int result = Calculator.Plus(x,y);
WriteLine(result);
x = 5;
y = 2;
result = Calculator.Minus(x, y);
WriteLine(result);
}
}
}
Main() ๋ฉ์๋ ์์์ Calculator.Plus() ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ฅผ ๋๊ธฐ๋ ์ฝ๋์์
x์ y๋ฅผ ์ธ์๋ก ๋๊ธฐ๊ณ ์๋ค.
์ธ์ x๋ Plus() ๋ฉ์๋์ ๋งค๊ฐ๋ณ์ a๋ก์, ์ธ์ y๋ ๋งค๊ฐ๋ณ์ b๋ก์ ๋๊ฒจ์ง๋ค.
์ด๊ฒ์ '๋ณต์ฌ'๋ผ๊ณ ํ๋ค!
namespace Hello
{
class Calculator
{
public static int Plus(int a, int b) //int a = x; int b = y;
{
WriteLine($"Input : {a},{b}");
int result = a + b;
return result;
}
public static int Minus(int a, int b) //int a = x; int b = y;
{
WriteLine($"Input : {a},{b}");
int result = a - b;
return result;
}
}
class MainApp
{
static void Main(string[] args)
{
int x = 3;
int y = 4;
int result = Calculator.Plus(x,y);
WriteLine(result);
x = 5;
y = 2;
result = Calculator.Minus(x, y);
WriteLine(result);
}
}
}
์ด๋, ์ธ์๋ก ๋๊ฒจ์ง x์ y๋ ์ค์ ๋ก Calculator.Plus() ๋ฉ์๋ ์์ผ๋ก ๋ค์ด๊ฐ๋ ๊ฒ์ ์๋๋ค.
๋งค๊ฐ ๋ณ์๋ ๊ทผ๋ณธ์ ์ผ๋ก๋ '๋ณ์'์ด๋ค.
๋ฉ์๋ ์ธ๋ถ๋ก๋ถํฐ ๋ฉ์๋ ๋ด๋ถ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ๋ฐ๋ ๋งค๊ฐ์ฒด ์ญํ ์ ํ๋๋ฐ,
์ด๋ ํ ๋ณ์๋ฅผ ๋ ๋ค๋ฅธ ๋ณ์์ ํ ๋นํ๋ฉด ๋ณ์๊ฐ ๋ด๊ณ ์๋ ๋ฐ์ดํฐ๊ฐ ๋ณต์ฌ๋๋ค.
(๊ทธ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ด๋ ์ฐธ์กฐ๋ ๊ฐ์)
Calculator.Plus()๋ฉ์๋๊ฐ ํธ์ถ๋ ๊ฒฝ์ฐ x๊ฐ ๋ด๊ณ ์๋ ๋ฐ์ดํฐ3์ ๋งค๊ฐ๋ณ์ a๋ก,
y๊ฐ ๋ด๊ณ ์๋ ๋ฐ์ดํฐ 4๋ ๋งค๊ฐ๋ณ์ b๋ก ๋ณต์ฌ๋๋ค.
์์ธํ ๋ถ๋ถ์ ๋ค์ ๊ธ์์ ๊ณ์...
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
17. C#์ ํด๋์ค (0) | 2022.02.10 |
---|---|
16. ๊ฐ, ์ฐธ์กฐ์ ์ํ ๋งค๊ฐ๋ณ์ ์ ๋ฌ, ref์ out์ ์ฐจ์ด (0) | 2022.02.09 |
14. ์ ์ถ๋ ฅ, ๋ถ๊ธฐ๋ฌธ (0) | 2022.02.08 |
13. null ๋ณํฉ ์ฐ์ฐ์ (0) | 2022.02.08 |
12. ๋นํธ ์ฐ์ฐ์ (0) | 2022.02.08 |