blog/content/post/2008-05-01-00000901.md

208 lines
11 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 『新版C言語プログラミングレッスン 入門編』第8章・関数
author: kazu634
date: 2008-05-01
url: /2008/05/01/_967/
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:3961;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
- Programming
---
<div class="section">
<h4>
関数の定義
</h4>
<p>
 Cの関数は
</p>
<blockquote title="http" cite="http://www.amazon.co.jp/%E6%96%B0%E7%89%88C%E8%A8%80%E8%AA%9E%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E3%83%AC%E3%83%83%E3%82%B9%E3%83%B3-%E5%85%A5%E9%96%80%E7%B7%A8-%E7%B5%90%E5%9F%8E%E6%B5%A9/dp/4797336781/ref=sr_1_10?ie=UTF8&s=books&qid=1209639049&sr=1-10">
<p>
0個以上の引数を持ち、0個か1個の戻り値を持ち、まとまった仕事を行うプラグラムの一部分」
</p>
<p>
<cite><a href="http://www.amazon.co.jp/%E6%96%B0%E7%89%88C%E8%A8%80%E8%AA%9E%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E3%83%AC%E3%83%83%E3%82%B9%E3%83%B3-%E5%85%A5%E9%96%80%E7%B7%A8-%E7%B5%90%E5%9F%8E%E6%B5%A9/dp/4797336781/ref=sr_1_10?ie=UTF8&s=books&qid=1209639049&sr=1-10" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/%E6%96%B0%E7%89%88C%E8%A8%80%E8%AA%9E%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E3%83%AC%E3%83%83%E3%82%B9%E3%83%B3-%E5%85%A5%E9%96%80%E7%B7%A8-%E7%B5%90%E5%9F%8E%E6%B5%A9/dp/4797336781/ref=sr_1_10?ie=UTF8&s=books&qid=1209639049&sr=1-10', 'http://www.amazon.co.jp/%E6%96%B0%E7%89%88C%E8%A8%80%E8%AA%9E%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E3%83%AC%E3%83%83%E3%82%B9%E3%83%B3-%E5%85%A5%E9%96%80%E7%B7%A8-%E7%B5%90%E5%9F%8E%E6%B5%A9/dp/4797336781/ref=sr_1_10?ie=UTF8&#038;s=books&#038;qid=1209639049&#038;sr=1-10:title');" target="_blank">http://www.amazon.co.jp/%E6%96%B0%E7%89%88C%E8%A8%80%E8%AA%9E%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E3%83%AC%E3%83%83%E3%82%B9%E3%83%B3-%E5%85%A5%E9%96%80%E7%B7%A8-%E7%B5%90%E5%9F%8E%E6%B5%A9/dp/4797336781/ref=sr_1_10?ie=UTF8&s=books&qid=1209639049&sr=1-10:title</a></cite>
</p>
</blockquote>
<p>
<a name="seemore"></a>
</p>
<h4>
3割引の計算をする関数
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">float</span> calc(<span class="synType">float</span> x);
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synComment">/* この部分が関数だよ */</span>
<span class="synType">float</span> calc(<span class="synType">float</span> x)
{
<span class="synStatement">return</span>(x * <span class="synConstant">0.7</span>);
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">float</span> y;
y = calc(<span class="synConstant">10000.0</span>);
printf (<span class="synConstant">&#34;値段 </span><span class="synSpecial">%.0f</span><span class="synConstant"> 円の時&#34;</span>, <span class="synConstant">10000.0</span>);
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%.0f</span><span class="synConstant"> 円支払います。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, y);
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
<p>
ちなみに Perl だと、こんな感じかな?
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$sum</span> = <span class="synConstant">10000</span>;
<span class="synIdentifier">$sum</span> = <span class="synIdentifier">&#38;calc</span>(<span class="synIdentifier">$sum</span>);
<span class="synStatement">print</span> <span class="synIdentifier">$sum</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</span>{
<span class="synStatement">my</span> <span class="synIdentifier">$i</span>;
<span class="synStatement">my</span> <span class="synIdentifier">@nums</span> = <span class="synIdentifier">@_</span>;
<span class="synIdentifier">$i</span> = <span class="synIdentifier">$nums</span>[<span class="synConstant"></span>] * <span class="synConstant">0.7</span>;
<span class="synStatement">return</span> <span class="synIdentifier">$i</span>;
}
</pre>
<p>
Cだと変数の方の宣言をする必要があって、結構がちがちにやる必要があります。それに対して、Perlだと引数の個数を指定する必要がなかったりと自由度高め。自分は配列の1番目添え字0に決めうちで計算をさせています。Perlの自由度は高いなー。
</p>
<p>
</p>
<h4>
戻り値のない関数
</h4>
<p>
 アスタリスクをn個表示させます。
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">void</span> prt_graph(<span class="synType">int</span> x);
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synType">void</span> prt_graph(<span class="synType">int</span> x)
{
<span class="synType">int</span> i;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; x;i++) {
printf (<span class="synConstant">&#34;*&#34;</span>);
}
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
prt_graph(<span class="synConstant">10</span>);
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
<p>
Perlだと、こんな感じになる。簡単だ。
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">print</span> <span class="synConstant">&#34;*&#34;</span> x <span class="synConstant">10</span>;
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
</pre>
<h4>
xのn乗を計算する
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synType">int</span> get_power(<span class="synType">int</span> x, <span class="synType">int</span> n);
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synType">int</span> get_power(<span class="synType">int</span> x, <span class="synType">int</span> n)
{
<span class="synComment">/* 変数の定義 */</span>
<span class="synComment">/* i はカウンタ */</span>
<span class="synComment">/* res は計算と戻り値のため */</span>
<span class="synType">int</span> i;
<span class="synType">int</span> res = <span class="synConstant">1</span>;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60;= n - <span class="synConstant">1</span>;i++) {
res = res * x;
}
<span class="synStatement">return</span>(res);
}
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> temp;
temp = get_power(<span class="synConstant">7</span>, <span class="synConstant">4</span>);
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, temp);
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
<p>
Perlだとこうなる。ただし、厳密な引数のチェックはしていない。
</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;get_power</span>(<span class="synConstant">10</span>,<span class="synConstant">3</span>);
<span class="synStatement">print</span> <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">sub</span><span class="synIdentifier"> get_power</span>{
<span class="synStatement">my</span> <span class="synIdentifier">$res</span> = <span class="synConstant">1</span>;
<span class="synStatement">my</span> (<span class="synIdentifier">$x</span>, <span class="synIdentifier">$n</span>) = <span class="synIdentifier">@_</span>;
<span class="synStatement">for</span> (<span class="synStatement">my</span> <span class="synIdentifier">$i</span> = <span class="synConstant"></span>; <span class="synIdentifier">$i</span> &#60;= <span class="synIdentifier">$n</span> - <span class="synConstant">1</span>; <span class="synIdentifier">$i</span>++) {
<span class="synIdentifier">$res</span> = <span class="synIdentifier">$res</span> * <span class="synIdentifier">$x</span>;
}
<span class="synStatement">return</span> <span class="synIdentifier">$res</span>;
}
</pre>
<h4>
練習問題
</h4>
<p>
 nの2乗個の*を表示させるnを10→0→10と変化させる
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synComment">/* プロトタイプ宣言 */</span>
<span class="synType">void</span> prt_graph(<span class="synType">int</span> n);
<span class="synType">int</span> main(<span class="synType">void</span>);
<span class="synComment">/* 関数 prt_graph */</span>
<span class="synType">void</span> prt_graph(<span class="synType">int</span> n)
{
<span class="synType">int</span> i;
<span class="synType">int</span> cnt;
cnt = n * n;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; cnt;i++) {
printf (<span class="synConstant">&#34;*&#34;</span>);
}
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synComment">/* メイン */</span>
<span class="synType">int</span> main(<span class="synType">void</span>)
{
<span class="synType">int</span> i;
<span class="synStatement">for</span> (i = <span class="synConstant">10</span>;<span class="synConstant"></span> &#60; i;i = i -<span class="synConstant">1</span>) {
prt_graph(i);
}
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60; <span class="synConstant">11</span>; i++) {
prt_graph(i);
}
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
</div>