# varargout

## 语法

``varargout``

## 描述

``varargout` 是函数定义语句中的一个输出变量，允许函数返回任意数量的输出参量。使用小写字符指定 `varargout`，将其作为最后一个输出参量附加在任何显式声明的输出后。在执行此函数时，`varargout` 是一个 1×N 元胞数组，其中 N 是显式声明的输出结果后请求的输出数量。在函数内部，`varargout` 是一个未初始化且未预分配的变量。`

## 示例

`type returnVariableNumOutputs`
```function [s,varargout] = returnVariableNumOutputs(x) nout = max(nargout,1) - 1; s = size(x); for k = 1:nout varargout{k} = s(k); end end ```

```A = rand(4,5,2); [s,rows,cols] = returnVariableNumOutputs(A)```
```s = 1×3 4 5 2 ```
```rows = 4 ```
```cols = 5 ```

```A = zeros(1,4,5,2); [s,dim1,dim2,dim3] = returnVariableNumOutputs(A)```
```s = 1×4 1 4 5 2 ```
```dim1 = 1 ```
```dim2 = 4 ```
```dim3 = 5 ```

`A` 再次调用该函数，并请求一个输出。现在函数返回 `A` 的维数，而不是 `varargout`

`s = returnVariableNumOutputs(A)`
```s = 1×4 1 4 5 2 ```

`type variableNumInputAndOutput`
```function varargout = variableNumInputAndOutput(varargin) disp(['Number of provided inputs: ' num2str(length(varargin))]) disp(['Number of requested outputs: ' num2str(nargout)]) for k = 1:nargout varargout{k} = k; end end ```

`[d,g,p] = variableNumInputAndOutput(6,'Nexus')`
```Number of provided inputs: 2 Number of requested outputs: 3 ```
```d = 1 ```
```g = 2 ```
```p = 3 ```

`variableNumInputAndOutput`
```Number of provided inputs: 0 Number of requested outputs: 0 ```