int foo(int x, int y)
{
return x + y;
} // x and y are destroyed here
int main()
{
int x = 1 , y= 2;
foo(6, 7); // 6, 7 arguments (actual parameters)
foo(x, y + 1);
return 0;
}
foo함수가 끝나는 지점에서foo함수의 매개변수로 선언된 x와 y는 함수가 종료되면서 없어진다.main함수 안에서foo함수가 호출되어 사용될 때 들어가는 것들을 인자 (argument) 라고 한다.
void doSomething(int y)
{
cout << "In func " << y << " " << &y << endl;
}
int main() {
doSomething(5);
int x = 6;
cout << "In main " << x << " " << &x << endl;
doSomething(x);
doSomething(x + 1);
return 0;
}
In func 5 0x7ffeefbff45c
In main 6 0x7ffeefbff478
In func 6 0x7ffeefbff45c
In func 7 0x7ffeefbff45c
doSomething함수의 파라미터로 값으로 전달되는 인자는 다른 변수의 값으로 전달될 때 값이 복사가 되어서 새로운 메모리 공간을 가진 값이 전달이 된다.- 결과를 보면 변수
x의 5를 함수의 인자로 사용하여 함수를 호출했을때, 변수 x의 주소와는 다르다.- 값으로 호출 시 함수안에서 사용하는 값은 변수에 영향을 미치지 않는다.
void addOne(int &y)
{
y = y + 1;
}
int main()
{
int x = 5;
cout << x << " " << &x << endl;
addOne(x);
cout << x << " " << &x << endl;
return 0;
}
5 0x7ffeefbff478
6 0x7ffeefbff478
- 참조에 의한 호출에서는 그냥 변수 자체를 넘기는 것이기 때문에 주소도 똑같고, 새로 인자를 넘기기 위한 복사 및 메모리할당도 일어나지 않는다.
#include <iostream>
#include <cmath> // sin(), cos()
using namespace std;
void getSinCos(const double degree, double &sin_out, double &cos_out)
{
// 함수 호출될 때마다 새로 정의되지 않고 한번만 될 수 있도록 static
static const double pi = 3.141592; // 헤더로 뽑을수도 있음
const double radians = degree * pi / 180.0; // degree 는 안바뀌므로 매개변수 선언시 const로
sin_out = std::sin(radians);
cos_out = std::cos(radians);
}
int main()
{
double sin(0.0);
double cos(0.0);
getSinCos(30.0, sin, cos);
cout << sin << " " << cos << endl;
return 0;
}
typedef int* pint;
//void foo(int *&ptr)
void foo(pint &ptr)
{
cout << ptr << " " << &ptr << endl;
}
int main()
{
int x = 5;
int *ptr = &x;
cout << ptr << " " << &ptr << endl;
foo(ptr);
return 0 ;
}
0x7ffeefbff478 0x7ffeefbff470
0x7ffeefbff478 0x7ffeefbff470
#include <iostream>
#include <vector>
using namespace std;
void printElement(vector<int> &arr)
//void printElement(int &arr[4])
{
}
int main()
{
// int arr[] {1, 2, 3, 4 };
vector<int> arr {1, 2, 3, 4};
printElement(arr);
return 0 ;
}
inline int min (int x, int y)
{
return x > y ? y : x;
}
int main()
{
cout << min(5, 6) << endl;
cout << min(3, 2) << endl;
// 컴파일러가 위에 코드를
// cout << (5 > 6 ? 6 : 5) << endl 라고 본다
return 0;
}
int add(int x, int y)
{
return x + y;
}
double add(double x, double y)
{
return x + y;
}
int main()
{
cout << add(1,2) << endl;
cout << add(3.0, 4.0) << endl;
return 0;
}
void print(int x = 10, int y = 20, int z = 30); // in header
void print(int x, int y, int z)
{
cout << x << " " << y << " " << z << endl;
}
int main()
{
print();
print(100);
print(100, 200);
print(100, 200, 300);
return 0;
}
10 20 30
100 20 30
100 200 30
100 200 300
#include <iostream>
#include <array>
using namespace std;
bool isEven(const int& number)
{
if(number % 2 == 0) return true;
else return false;
}
bool isOdd(const int& number)
{
if(number % 2 != 0) return true;
else return false;
}
void printNum(const array<int, 10>& my_array, bool (*check_fcn)(const int&) = isEven) // 함수 포인터 기본값 선언
{
for(auto element : my_array)
{
if(check_fcn(element) == true) cout << element;
}
cout << endl;
}
int main()
{
std::array<int, 10> my_arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
printNum(my_arr);
printNum(my_arr, isOdd);
return 0;
}
int main()
{
std::vector<int> v { 1, 2, 3 };
v.resize(2);
for(auto &e : v)
cout << e << " ";
cout << endl;
cout << v.size() << " " << v.capacity() << endl;
int *ptr = v.data();
cout << ptr[2] << endl;
return 0;
}
1 2
2 3
3메모리에 실제로 값은 저장되어있지만, vector 는 없는척 한다.
(vector).puch_back(); : 쌓기(vector).pop_back(); : 맨위 뽑기#include <iostream>
#include <vector>
using namespace std;
void printStack(const std::vector<int> &stack)
{
for (auto &e : stack)
cout << e << " ";
cout << endl;
}
int main()
{
std::vector<int> stack;
// stack.reserve(1024); // 메모리의 용량을 미리 확보한다, size에는 포함 안된다.
stack.push_back(3);
printStack(stack);
stack.push_back(5);
printStack(stack);
stack.push_back(7);
printStack(stack);
stack.pop_back();
printStack(stack);
stack.pop_back();
printStack(stack);
stack.pop_back();
printStack(stack);
return 0;
}
3
3 5
3 5 7
3 5
3
void countDown(int count)
{
cout << count << endl;
if(count > 0)
countDown(count - 1);
}
int main() {
countDown(10);
return 0;
}
int sumTo(int sumto)
{
if (sumto <= 0)
return 0;
else if (sumto <= 1)
return 1;
else
return sumTo(sumto - 1) + sumto;
}
int main() {
cout << sumTo(5) << endl;
return 0;
}
피보나치
#include <iostream>
using namespace std;
int fibonacci(int num)
{
if (num <= 1)
return num;
else
{
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
int main() {
cout << fibonacci(15) << endl;
return 0;
}