blog/content/post/2008/03/09/2008-03-09-『新版perl言語プログラミング...

68 lines
3.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: '『[新版Perl言語プログラミングレッスン入門編』第10章 サブルーチン'
author: kazu634
date: 2008-03-09
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:3805;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
- Programming
---
<div class="section">
<p>
 今日はサブルーチンだよー。
</p>
<p>
<a name="seemore"></a>
</p>
<h4>
簡単なサブルーチン
</h4>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">my</span> (<span class="synIdentifier">$x</span>, <span class="synIdentifier">$y</span>) = (<span class="synConstant">123</span>, <span class="synConstant">456</span>);
<span class="synIdentifier">&#38;print_xy</span>(<span class="synIdentifier">$x</span>, <span class="synIdentifier">$y</span>);
<span class="synStatement">sub</span><span class="synIdentifier"> print_xy </span>{
<span class="synStatement">my</span> (<span class="synIdentifier">$x</span>, <span class="synIdentifier">$y</span>) = <span class="synIdentifier">@_</span>;
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synSpecial">\$</span><span class="synConstant">x = </span><span class="synIdentifier">$x</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synSpecial">\$</span><span class="synConstant">y = </span><span class="synIdentifier">$y</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
}
</pre>
<p>
&&#8230;」でサブルーチン呼び出しという意味になる。「@_」は引数のリストという意味かな
</p>
<h4>
戻り値のあるサブルーチン
</h4>
<p>
 戻り値のあるサブルーチンは、returnを用いると実現できるよ
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">print</span> <span class="synIdentifier">&#38;calc_ave</span>(<span class="synConstant">100</span>, <span class="synConstant">50</span>, <span class="synConstant">30</span>, <span class="synConstant">20</span>) , <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">sub</span><span class="synIdentifier"> calc_ave </span>{
<span class="synStatement">my</span> <span class="synIdentifier">@nums</span> = <span class="synIdentifier">@_</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$sum</span>;
<span class="synStatement">foreach</span> (<span class="synIdentifier">@nums</span>) {
<span class="synIdentifier">$sum</span> = <span class="synIdentifier">$sum</span> + <span class="synIdentifier">$_</span>;
}
<span class="synStatement">my</span> <span class="synIdentifier">$ave</span> = <span class="synIdentifier">$sum</span> / <span class="synIdentifier">@nums</span>;
<span class="synStatement">return</span> <span class="synIdentifier">$ave</span>
}
</pre>
<p>
本に載っているやつだと汎用的じゃあなかったから、引数が何個でも大丈夫なようにしてみた。直前の章のコンテクストがうまく理解できている気がする( my $ave = $sum / @nums。この場合スカラーコンテクストだから、@numsは配列の要素数になるんだよね
</p>
</div>