第4週C言語及びプログラム設計向上ルーチン-8関数の宣言、定義及び呼び出し
2070 ワード
呼び出された条件-呼び出された関数は既に存在します
カスタム関数を定義してから呼び出す
#include <stdio.h>
#include <math.h>
float max(float x, float y);
int main ()
{
float a,b,c,s;
scanf("%f %f", &a, &b);
s=sqrt(a);
printf("sqrt is %.2f
", s);
c=max(a+b, a*b) ;
printf("max is %.f
", c);
return 0;
}
float max(float x, float y)
{
float z;
z=(x>y)? x : y ;
return z;
}
カスタム関数を定義してから呼び出す
#include <stdio.h>
float max(float x, float y)
{
float z;
z=(x>y)? x : y ;
return z;
}
<pre code_snippet_id="602005" snippet_file_name="blog_20150210_2_796146" name="code" class="cpp" style="white-space: pre-wrap; word-wrap: break-word;"><span style="font-family: Arial, Helvetica, sans-serif;">int main ()</span>
{ float a,b,c; scanf("%f %f", &a, &b); c=max(a+b, a*b) ; printf("max is %.f", c); return 0;}
若要先调用,后定义:调用前声明
#include <stdio.h> float max(float, float); int main () { float a,b,c; scanf("%f %f", &a, &b); c=max(a+b, a*b) ; printf("max is %.f
", c); return 0; } float max(float x, float y) { float z; z=(x>y)? x : y ; return z; }
コーディング仕様:関数定義前のコメント/* : : , : : */ int gcd(int n1, int n2) { int r; while(n2!=0) { r=n1%n2; n1=n2; n2=r; } return n1; }