// 샘플코드 20
#include <iostream>
using namespace std;
int add(int x, int y) {
return x + y;
}
int main() {
int result = add(1, 2);
cout << "The result is:" << result << "\n";
cout << "Adding 3 and 4 gives us:" << add(3, 4);
}
// 샘플코드 21
#include <iostream>
using namespace std;
void changeArgument(int x) {
x = x + 5;
}
int main() {
int y = 4;
changeArgument(y);
cout << y;
}
// 샘플 코드 22
#include <iostream>
using namespace std;
// 영역을 설명하기 위한 함수
int doStuff()
{
return 2 + 3;
}
// 전역 변수도 다른 변수들 처럼 초기화 한다
int count_of_function_calls = 0;
void fun()
{
// 이곳에서도 전역 변수를 사용할 수 있고
count_of_function_calls++;
}
void main() {
fun();
fun();
fun();
// 이곳에서도 전역 변수를 사용할 수 있다
cout << "Function fun was called"
<< count_of_function_calls << " times";
}
// 샘플 코드 23
#include <iostream>
using namespace std;
int main()
{
int result = add(1, 2);
cout << "The result is:" << result << "\n";
cout << "Adding 3 and 4 gives us:" << add(3, 4);
}
int add(int x, int y)
{
return x + y;
}
// 샘플 코드 24
#include <iostream>
using namespace std;
// 함수 선언
int add(int x,int y);
int main()
{
int result = add(1, 2);
cout << "The result is:" << result << "\n";
cout << "Adding 3 and 4 gives us:" << add(3, 4);
}
int add(int x, int y)
{
return x + y;
}
'C++' 카테고리의 다른 글
[C++] CHAPTER 7 샘플 코드 (0) | 2022.05.30 |
---|---|
[C++] CHAPTER 6 실습 과제 3 (1) | 2022.05.30 |
[C++] CHAPTER 5 샘플 코드 (0) | 2022.05.27 |
[C++] CHAPTER 5 실습 과제 3 (1) | 2022.05.27 |
[C++] CHAPTER 4 샘플 코드 (0) | 2022.05.27 |