1 / 5

void swap0 ( int x, int y ){ int t; t = x ; x = y ; y = t; }

http://www.ablmcc.edu.hk/~ scy/home/javascript/function.htm. Call by Value 按 值 調用. void swap0 ( int x, int y ){ int t; t = x ; x = y ; y = t; }. Call by Reference 按 址 調用. x=10, y=20. void swap ( int *x , int *y ){ int t; t = *x ; *x = *y ; *y = t; }. x=address

kuniko
Download Presentation

void swap0 ( int x, int y ){ int t; t = x ; x = y ; y = t; }

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. http://www.ablmcc.edu.hk/~scy/home/javascript/function.htm Call by Value按值調用 void swap0 (int x, int y){ int t; t = x; x = y; y = t; } Call by Reference按址調用 x=10, y=20 void swap (int*x, int*y){ int t; t = *x; *x = *y; *y = t; } x=address *x=value main(){ int a=10, b=20; swap0 (a,b); printf("a=%i, b=%i\n", a,b); swap (&a, &b); printf("a=%i, b=%i\n", a,b); } x y 20 10 a=10, b=20 a=20, b=10

  2. Call by Value按值調用 void drawLine (char c, int n){ inti; for (i=0; i<n;i++) printf("%c", c); printf("\n"); } main(){ drawLine ('*',20); … drawLine('-',10); } ******************** ----------

  3. Call by Reference按址調用 address of name void allUpper (char s[]){ inti; for (i=0; i<strlen(s); i++) s[i] = toupper(s[i]); } CHAN TAI MAN, JOSEPH main(){ char name[50]="chan tai man, joseph"; allUpper(name); } changes to s  changes to name s C H A N scanf("%s", & name); address s and name share the same address

  4. printf(" x=%i\n", x); printf("*y=%i\n", *y); // error int x=10; int*y; y = &x; printf("*y=%i\n", *y); y = malloc(sizeof(int)); *y = 100; printf("*y=%i\n", *y);

  5. intsumN (int n){ if(n<=0) return 0; return n+sumN(n-1); } main(){ int a=10, b=20, c=30; c = sumN (a); printf("c=%i\n", c); }

More Related