多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 第1.3函数 - 函数定义,支持多返回值,返回值初始化0, ``` <pre class="calibre16">``` func <span class="token1">hypot</span><span class="token2">(</span>x<span class="token2">,</span> y float64<span class="token2">)</span> float64 <span class="token2">{</span> <span class="token5">return</span> math<span class="token2">.</span><span class="token1">Sqrt</span><span class="token2">(</span>x<span class="token">*</span>x <span class="token">+</span> y<span class="token">*</span>y<span class="token2">)</span> <span class="token2">}</span> func <span class="token1">typedTwoValues</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">(</span>int<span class="token2">,</span> int<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token3">1</span><span class="token2">,</span> <span class="token3">2</span> <span class="token2">}</span> 匿名函数<span class="token2">,</span>传递后可以调用 f <span class="token2">:</span><span class="token">=</span> <span class="token1">func</span><span class="token2">(</span>data int<span class="token2">)</span> <span class="token2">{</span> fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span><span class="token4">"hello"</span><span class="token2">,</span> data<span class="token2">)</span> <span class="token2">}</span> 可变参数 func <span class="token1">myfunc</span><span class="token2">(</span>args <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span>int<span class="token2">)</span> <span class="token2">{</span> <span class="token5">for</span> _<span class="token2">,</span> arg <span class="token2">:</span><span class="token">=</span> range args <span class="token2">{</span> fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span>arg<span class="token2">)</span> <span class="token2">}</span> <span class="token2">}</span> 递归阶乘 func <span class="token1">Factorial</span><span class="token2">(</span>n uint64<span class="token2">)</span> <span class="token2">(</span>result uint64<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> n <span class="token">></span> <span class="token3">0</span> <span class="token2">{</span> result <span class="token">=</span> n <span class="token">*</span> <span class="token1">Factorial</span><span class="token2">(</span>n<span class="token">-</span><span class="token3">1</span><span class="token2">)</span> <span class="token5">return</span> result <span class="token2">}</span> <span class="token5">return</span> <span class="token3">1</span> <span class="token2">}</span> ``` ```