MATLAB Language                
            기능들
        
        
            
    수색…
기본 예제
다음 MATLAB 스크립트는 기본 함수를 정의하고 호출하는 방법을 보여줍니다.
myFun.m :
    function [out1] = myFun(arg0, arg1)
        out1 = arg0 + arg1;
    end
터미널 :
    >> res = myFun(10, 20)
    res =
        30
다중 출력
다음 MATLAB 스크립트는 단일 함수에서 여러 출력을 반환하는 방법을 보여줍니다.
myFun.m :
    function [out1, out2, out3] = myFun(arg0, arg1)
        out1 = arg0 + arg1;
        out2 = arg0 * arg1;
        out3 = arg0 - arg1;
    end
터미널 :
    >> [res1, res2, res3] = myFun(10, 20)
    res1 =
            30
    res2 =
            200
    res3 =
            -10
그러나 MATLAB은 단일 변수에 할당 된 경우 첫 번째 값만 반환합니다.
    >> res = myFun(10, 20)
    res =
            30
다음 예제는 특정 출력을 얻는 방법을 보여줍니다.
    >> [~, res] = myFun(10, 20)
    res =
            200
경계선
 함수의 본문에서 nargin 과 nargout 은 각각 호출에 제공된 실제 입출력 수를 나타냅니다. 
예를 들어 제공된 입력 수를 기반으로 함수의 실행을 제어 할 수 있습니다.
myVector.m :
function [res] = myVector(a, b, c)
    % Roughly emulates the colon operator
    switch nargin
        case 1
            res = [0:a];
        case 2
            res = [a:b];
        case 3
            res = [a:b:c];
        otherwise
            error('Wrong number of params');
    end
end
단말기:
>> myVector(10)
ans =
    0    1    2    3    4    5    6    7    8    9   10
>> myVector(10, 20)
ans =
   10   11   12   13   14   15   16   17   18   19   20
>> myVector(10, 2, 20)
ans =
   10   12   14   16   18   20
비슷한 방법으로 우리는 출력 매개 변수의 수를 기반으로 함수의 실행을 제어 할 수 있습니다.
myIntegerDivision :
function [qt, rm] = myIntegerDivision(a, b)
    qt = floor(a / b);
    if nargout == 2
        rm = rem(a, b);
    end
end
터미널 :
>> q = myIntegerDivision(10, 7)
q = 1
>> [q, r] = myIntegerDivision(10, 7)
q = 1
r = 3
Modified text is an extract of the original Stack Overflow Documentation
        아래 라이선스 CC BY-SA 3.0
        와 제휴하지 않음 Stack Overflow