// 함수 선언 (함수 원형)
반환타입 함수명(매개변수);
// 함수 정의
반환타입 함수명(매개변수) {
// 함수 본체
return 값;
}
// 함수 호출
결과 = 함수명(인수);
#include <iostream>
using namespace std;
// 함수 원형 선언
int max(int x, int y);
int main() {
int result = max(10, 20); // 함수 호출
cout << "최댓값: " << result << endl;
return 0;
}
// 함수 정의
int max(int x, int y) {
if (x > y)
return x;
else
return y;
}
// 값으로 전달
void func1(int x) {
x = 100; // 원본 변경 안됨
}
// 참조로 전달
void func2(int& x) {
x = 100; // 원본 변경됨
}
#include <iostream>
using namespace std;
void swap(int& x, int& y) {
int temp = x;
x = y;
y = temp;
}
int main() {
int a = 100, b = 200;
cout << "교환 전: a=" << a << ", b=" << b << endl;
swap(a, b);
cout << "교환 후: a=" << a << ", b=" << b << endl;
return 0;
}
#include <iostream>
using namespace std;
int square(int i) {
cout << "정수 제곱 함수 호출" << endl;
return i * i;
}
double square(double i) {
cout << "실수 제곱 함수 호출" << endl;
return i * i;
}
void print(int i) { cout << "정수: " << i << endl; }
void print(double f) { cout << "실수: " << f << endl; }
void print(char c) { cout << "문자: " << c << endl; }
반환타입 함수명(타입 변수1, 타입 변수2 = 기본값, 타입 변수3 = 기본값);
#include <iostream>
using namespace std;
void display(char c = '*', int n = 10) {
for (int i = 0; i < n; i++)
cout << c;
cout << endl;
}
int sum(int x, int y, int z = 0, int w = 0) {
return x + y + z + w;
}
int main() {
display(); // 모든 기본값 사용
display('#'); // c='#', n=10(기본값)
display('#', 5); // 모든 값 지정
cout << sum(10, 20) << endl; // 30
cout << sum(10, 20, 30) << endl; // 60
cout << sum(10, 20, 30, 40) << endl; // 100
return 0;
}
inline 키워드를 사용하여 함수 호출 오버헤드를 줄임inline double square(double i) {
return i * i;
}
inline int max(int a, int b) {
return (a > b) ? a : b;
}
<string> 헤더 파일 포함 필요#include <string>
using namespace std;
string s; // 빈 문자열 생성
string s = "Hello"; // 초기화
string s{"Hello"}; // 보편적 초기화
// 문자열 연산
string s1 = "Hello";
string s2 = "World";
string s3 = s1 + " " + s2; // 문자열 결합
s3 += "!"; // 문자열 추가
// 문자열 비교
if (s1 == s2) { } // 동등 비교
if (s1 > s2) { } // 사전식 비교
// 주요 멤버 함수
s.length() // 문자열 길이
s.find("text") // 부분 문자열 찾기
s[i] // i번째 문자 접근
getline(cin, s) // 공백 포함 문자열 입력
#include <iostream>
#include <string>
using namespace std;
int main() {
string name, address;
cout << "이름을 입력하세요: ";
cin >> name;
cin.ignore(); // 버퍼 정리
cout << "주소를 입력하세요: ";
getline(cin, address);
cout << address << "의 " << name << "씨 안녕하세요!" << endl;
// 문자열 검색
string text = "When in Rome, do as the Romans.";
int pos = text.find("Rome");
cout << "Rome의 위치: " << pos << endl;
return 0;
}
// 다음 코드의 출력 결과를 예측하시오
#include <iostream>
using namespace std;
int calculate(int a, int b = 5) {
return a * b;
}
int main() {
cout << calculate(3) << endl;
cout << calculate(3, 2) << endl;
return 0;
}
정답: 15, 6
해설: 첫 번째 호출에서는 b의 기본값 5가 사용되어 3×5=15, 두 번째 호출에서는 b=2가 전달되어 3×2=6
// 다음 코드를 완성하여 두 변수의 값을 교환하는 함수를 작성하시오
#include <iostream>
using namespace std;
void swap(_______ a, _______ b) {
_______________________
_______________________
_______________________
}
int main() {
int x = 10, y = 20;
cout << "교환 전: x=" << x << ", y=" << y << endl;
swap(x, y);
cout << "교환 후: x=" << x << ", y=" << y << endl;
return 0;
}
정답:
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
// 다음과 같이 동작하는 중복 함수 print를 작성하시오
// print(100) → "정수: 100"
// print(3.14) → "실수: 3.14"
// print('A') → "문자: A"
#include <iostream>
using namespace std;
// 여기에 중복 함수들을 작성하시오
_________________________________
_________________________________
_________________________________
int main() {
print(100);
print(3.14);
print('A');
return 0;
}
정답:
void print(int i) {
cout << "정수: " << i << endl;
}
void print(double f) {
cout << "실수: " << f << endl;
}
void print(char c) {
cout << "문자: " << c << endl;
}
// 사용자로부터 문자열을 입력받아 특정 문자의 개수를 세는 프로그램을 완성하시오
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
char target;
int count = 0;
cout << "문자열을 입력하세요: ";
getline(cin, text);
cout << "찾을 문자를 입력하세요: ";
cin >> target;
// 여기에 문자 개수를 세는 코드를 작성하시오
_________________________________
_________________________________
_________________________________
cout << "'" << target << "' 문자의 개수: " << count << endl;
return 0;
}
정답:
for (int i = 0; i < text.length(); i++) {
if (text[i] == target) {
count++;
}
}
또는:
for (char c : text) {
if (c == target) {
count++;
}
}
// 두 DNA 문자열의 해밍 거리를 구하는 프로그램을 작성하시오
// 해밍 거리: 같은 위치에서 다른 문자의 개수
#include <iostream>
#include <string>
using namespace std;
int hammingDistance(string s1, string s2) {
// 여기에 해밍 거리를 계산하는 코드를 작성하시오
_________________________________
_________________________________
_________________________________
}
int main() {
string dna1, dna2;
cout << "DNA1: ";
cin >> dna1;
cout << "DNA2: ";
cin >> dna2;
if (dna1.length() != dna2.length()) {
cout << "오류: 길이가 다릅니다." << endl;
} else {
int distance = hammingDistance(dna1, dna2);
cout << "해밍 거리: " << distance << endl;
}
return 0;
}
정답:
int hammingDistance(string s1, string s2) {
int count = 0;
for (int i = 0; i < s1.length(); i++) {
if (s1[i] != s2[i]) {
count++;
}
}
return count;
}
& 연산자를 사용했는가?#include <string> 헤더를 포함했는가?getline()을 사용했는가?cin 후 getline() 사용 시 cin.ignore()를 호출했는가?length() 또는 size() 함수로 구했는가?팩토리얼을 계산하는 함수를 작성하고, 중복 함수로 int와 long long 타입을 모두 처리하도록 하시오.
문자열에서 모든 공백을 제거하는 함수를 작성하시오.
두 문자열이 애너그램(같은 글자로 구성되었지만 순서가 다른 단어)인지 판별하는 함수를 작성하시오.