1 / 4

無傳回值 , 無參數的副程式

副程式 ( Subroutine ). 無傳回值 , 無參數的副程式. void pabc() { printf(&quot;abc<br>&quot;); } void pdef() { printf(&quot;def<br>&quot;); }. #include &lt;stdio.h&gt; #include &lt;conio.h&gt; void pabc(); void pdef(); void main() { int i,n; long sum=0; clrscr(); pabc(); pdef(); }. 無傳回值 , 有參數的副程式. #include &lt;stdio.h&gt;

briar
Download Presentation

無傳回值 , 無參數的副程式

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 副程式 (Subroutine) • 無傳回值,無參數的副程式 void pabc() { printf("abc\n"); } void pdef() { printf("def\n"); } #include <stdio.h> #include <conio.h> void pabc(); void pdef(); void main() { int i,n; long sum=0; clrscr(); pabc(); pdef(); }

  2. 無傳回值,有參數的副程式 #include <stdio.h> #include <conio.h> void print_add(int a, int b); void main() { clrscr(); print_add(5,7); } void print_add(int a,int b) { printf(“sum is:”,a+b); } #include <stdio.h> #include <conio.h> void sub1(int a, int b); void main() { int c=5;d=9; clrscr(); sub1(c,d); } void sub1(int a,int b) { a++; b++; }

  3. /* find 1!+2! + ... +5!=153 */ /* not using recursive */ #include <stdio.h> #include <conio.h> unsigned int fact(int n); void main() { unsigned int sum=0; int i; for (i=1;i<=5;i++) sum+=fact(i); clrscr(); printf("1!+2!+...+5!=%d",sum); } unsigned int fact(int n) { unsigned int product=1; int times; for (times=1;times<=n;times++) product*=times; return(product); } • 有傳回值,有參數的副程式 #include <stdio.h> #include <conio.h> int sum(int a, int b); void main() { int k; clrscr(); k=sum(5,7)+1; } int sum(int a, int b) { return(a+b); }

  4. 遞迴 #include <stdio.h> #include <conio.h> unsigned int fact(int n); /* declare function */ void main() { clrscr(); int k; char pause; printf("please input a integer:"); scanf("%d",&k); printf("%d!=",k); printf("%u\n",fact(k)); printf("press any key to continue..."); pause=getche(); } unsigned int fact(int n) { if (n==1) { printf("1="); return(1); } else { printf("%d*",n); return(n*fact(n-1)); } }

More Related