https://youtu.be/5JR5ydzEX3A?feature=shared

https://youtu.be/Pc7ain6E37I?feature=shared

C++ Return문, 포인터, 배열 응용

Return 키워드

return문은 함수에서 값을 반환하고 함수를 종료하는 키워드입니다.

#include <iostream>
#include <cpltasks.h>
using namespace std;

int Add(int a, int b)
{
    return a + b;
}

int main()
{
    int a = 5;
    int b = 6;

    int ret = Add(a, b);

    return 0;
}

return문의 역할:

숫자채우기 연습

번호 순서로 숫자를 배열에 채우기

x는 0 ~ 2까지, y는 0 ~ 2까지 반복하며 2차원 배열에 순차적으로 값을 저장합니다.

#include <iostream>
using namespace std;

int main()
{
    int vect[3][3] = { 0 };

    int x, y;
    int t = 1;
    for (y = 0; y < 3; y++)
    {
        for (x = 0; x < 3; x++)
        {
            vect[y][x] = t;
            t++;
        }
    }

    return 0;
}

결과:

1 2 3
4 5 6
7 8 9

역순으로 숫자 채우기

y방향이 바깥에 움직이고, x방향이 안쪽에 움직입니다. x는 2부터 0까지 움직이고, y는 0부터 2까지 움직입니다.

#include <iostream>
using namespace std;

int main()
{
    int vect[3][3] = { 0 };

    int x, y;
    int t = 1;

    for (x = 2; x >= 0; x--)
    {
        for (y = 0; y < 3; y++)
        {
            vect[y][x] = t;
            t++;
        }
    }

    return 0;
}

결과: