Introduction to the C Programming Language

Slides:



Advertisements
Similar presentations
CSIM, PU C Language Introduction to the C Programming Language 重覆敘述 (for,while,break,continue) 適合重複性的計算或判斷.
Advertisements

第一單元 建立java 程式.
Loops.
Chapter 5 遞迴 資料結構導論 - C語言實作.
Chapter 7 Subroutine and Function
Introduction to the C Programming Language
C语言程序设计 课程 第5章 数组 主讲:李祥 博士、副教授 单位:软件学院软件工程系.
第十一章 結構.
Visual C++ introduction
簡易C++除錯技巧 長庚大學機械系
選擇排序法 通訊一甲 B 楊穎穆.
函數(一) 自訂函數、遞迴函數 綠園.
Introduction to the C Programming Language
String C語言-字串.
Introduction to the C Programming Language
Introduction to the C Programming Language
STRUCTURE 授課:ANT 日期:2010/5/12.
Introduction to the C Programming Language
C語言簡介 日期 : 2018/12/2.
Function.
程式撰寫流程.
類別(class) 類別class與物件object.
SQL Stored Procedure SQL 預存程序.
Chap 8 指针 8.1 寻找保险箱密码 8.2 角色互换 8.3 冒泡排序 8.4 电码加密 8.5 任意个整数求和*
CLASS 5 指標.
C 程式設計— 函式 台大資訊工程學系 資訊系統訓練班.
Chapter 7 指標.
Methods 靜宜大學資工系 蔡奇偉副教授 ©2011.
Introduction to the C Programming Language
C语言 程序设计基础与试验 刘新国、2012年秋.
Introduction to the C Programming Language
第八章 使用指针.
計數式重複敘述 for 迴圈 P
Introduction to the C Programming Language
第5讲 结构化程序设计(Part II) 周水庚 2018年10月11日.
第七章 函数及变量存贮类型 7.1 函数基础与C程序结构 7.2 函数的定义和声明 7.3 函数的调用 7.4 函数的嵌套与递归
第一單元 建立java 程式.
|07 函數.
C语言概述 第一章.
輸入&輸出 函數 P20~P21.
第十章 指標.
Introduction to the C Programming Language
函数 概述 模块化程序设计 基本思想:将一个大的程序按功能分割成一些小模块, 特点: 开发方法: 自上向下,逐步分解,分而治之
Introduction to the C Programming Language
輸出與輸入(I/O).
C qsort.
第二章 类型、对象、运算符和表达式.
Introduction to the C Programming Language
Introduction to the C Programming Language
陣列與結構.
挑戰C++程式語言 ──第9章 函數.
Introduction to the C Programming Language
第七章  数 组.
實習八 函式指標.
Chap 6 函數 故用兵之法,十則圍之,五則攻之,倍則分之, 敵則能戰之,少則能逃之,不若則能避之。 故小敵之堅,大敵之擒也。
第四章 陣列、指標與參考 4-1 物件陣列 4-2 使用物件指標 4-3 this指標 4-4 new 與 delete
Introduction to the C Programming Language
Programming & Language Telling the computer what to do
Introduction to the C Programming Language
Chapter 6 函數.
Introduction to the C Programming Language
ABAP Basic Concept (2) 運算子 控制式與迴圈 Subroutines Event Block
Array(陣列) Anny
第三章 流程控制 程序的运行流程 选择结构语句 循环结构语句 主讲:李祥 时间:2015年10月.
C語言程式設計 老師:謝孟諺 助教:楊斯竣.
Introduction to the C Programming Language
微 處 理 機 專 題 – 8051 C語言程式設計 主題:階乘計算
方法(Method) 函數.
ABAP Basic Concept (2) 運算子 控制式與迴圈 Subroutines Event Block
InputStreamReader Console Scanner
Presentation transcript:

Introduction to the C Programming Language 續 Function (函式)

回顧複習

回顧-Function 宣告函式的語法格式 使用函式的語法格式 宣告函式1 函式1 主程式 主程式 函式1 /*當函式位於main()之前時, 不需宣告函式, 反之則要*/ 傳回值型態 function_name (資料型態1,資料型態2…); 宣告函式1 主程式 函式1 函式1 主程式 傳回值資料型態 函數名稱(傳入值資料型態 變數名稱, 傳入值資料型態 變數名稱 …) { parameters declarations; /*區域變數宣告*/ statements; /*函式主體*/ return 傳回值; /*若需傳回值時*/ }

Example 範例: 寫兩個函式,一個使兩數相加,傳回運算後結果;一個顯示答案,不傳回值. #include <stdio.h> int add(int,int);                           /*宣告函式,函式原型 int是傳回int型態,傳入兩個int型態 */ void show(int);                          /*宣告函式,函式原型 void是不傳回值,傳入一個int型態 */  void main() { int nNum1 = 5 , nNum2 = 5 , nSum; nSum = add(nNum1,nNum2);       /*把add(nNum1,nNum2)的return傳回nSum */ show(nSum);                                 /*把nSum傳入show函式 */  }   /*傳入的值是nNum1,nNum2,定義兩個int且nData1 = nNum1;nNum2 = nData2; */  int add(int nData1,int nData2)        return (nData1 + nData2);        /*傳回nData1 + nData2相加的值. */ /*傳入的是nSum,定義一個int nData,且nData= nSum; */  void show(int nData) printf("The Anser is %d" , nData); } f_ex.c add 與 show 兩函式於此程式中的任何地方均可被呼叫

練習 宣告兩個 function(函式),並傳遞3個整數: 第1個函式:將3個整數加總並回傳,在主程式印出。 第2個函式:將3個整數相乘,不用回傳,在函式中印出。

回顧-參數 全面性變數(Global variable) : 宣告在主程式區域外的變數, 可在程式中任何區域中存取該變數. 區域性變數(Local variable) : 宣告在主函數區域中或函數區域中的變數, 只在該宣告的區域中有效. 離開該區域則視為未定義變數, 各區域中的區域變數互不相干. #include<stdio.h> /*前置處理,呼叫標頭檔*/ 宣告函式 宣告變數 /*全域變數*/ int main( ) { 宣告變數 /*區域變數*/ return 0; /*傳回值*/ } 當全面性變數名稱與區域性變數名稱相同時, 在該區域範圍中以區域性的變數為主

續……函數之參數傳遞方式 進入本週主題

參數傳遞方式 傳值呼叫 (Call by value) 呼叫函式時, 將函式所需的資料值傳入. 傳址呼叫 (Call by address) 呼叫函式時, 將參數的位址傳入函式中, 因此函式宣告時, 參數為指標型態, 以便儲存參數的位址值. 常見使用傳址呼叫之狀況 : 當函式回傳值不只一個時 將陣列傳入函式時 將字串傳入函式時

傳值呼叫 - 範例一 比較使用者所輸入兩整數之大小 傳值呼叫參數值 5 傳回值 3 傳回值 函式呼叫 #include <stdio.h> int larger_value(int num_x, int num_y) { if(num_x < num_y) return(num_y); else return(num_x); } void main(void) int larger_value(int, int); int num_a, num_b; printf("\1: Please input 2 numbers. \n ==> "); scanf("%d %d”, &num_a, &num_b); printf(“The larger number is %d \n”, larger_value(num_a, num_b)); 5 3 num_a num_b 複製值 num_x num_y 傳值呼叫參數值 傳回值 傳回值 f1.c : Please input 2 numbers. 5 6 ==> The larger number is 6 函式宣告寫在主程式(即大括號)中, 表示該函式只可在該程式區塊中使用 若函式宣告寫在主程式與函式外 (即 #include) 後, 則該函式於有效範圍內 (接下來的程式範圍)都可呼叫該函式 函式呼叫

傳值呼叫 -範例二 輸入一數值 n, 求出 1 至 n 的連乘 (示範函數傳回數值給呼叫程式) 函式呼叫 (無參數傳遞) 參數列中無資料 void main() { long fact(void); long f; f = fact(); printf("The factorial = %ld \n", f); } long fact(void) int i,n; long f=1; printf("Enter one value : "); scanf("%d", &n); for( i=1; i<= n; i++) f *= i; return(f); 函式呼叫 (無參數傳遞) 參數列中無資料 f2.c Enter one value : 5 The factorial = 120 傳回值

傳值呼叫 -範例三 輸入一數值 n, 求出 1 至 n 的連乘 (示範由呼叫程式傳遞一數值給函數, 函數計算後將結果傳回給呼叫程式) void main() { int n; long f; long fact(int); printf("Enter one value : "); scanf("%d", &n); f = fact(n); printf("The factorial of %d = % ld \n", n, f); } long fact( int n) int i; long f = 1; for (i =1; i <= n; i++) f *= i; return (f); 函式呼叫 (參數傳遞) 參數列中傳遞之參數 f3.c Enter one value : 5 The factorial of 5 = 120 傳回值

傳值呼叫 -範例四 – (共2頁) 計算輸入之三個整數的 gcd最大公因數 與 lcm最小公倍數. void main() { int gcd(int, int); int lcm(int, int); int a, b, c; printf("Enter three integer values : "); scanf("%d %d %d", &a, &b, &c); printf("The gcd of %d %d %d is %d \n", a, b, c, gcd(gcd(a,b), c) ); printf("The lcm of %d %d %d is %d \n", a, b, c, lcm(lcm(a,b), c) ); } 續下頁 f4.c Enter three integer values : 5 9 3 The gcd of 5 9 3 is 1 The lcm of 5 9 3 is 45

傳值呼叫 -範例四 – 續 接上頁 int gcd(int x, int y) { int r; while (y != 0) r = x % y; x = y; y = r; } return(x); int lcm(int x, int y) return(x * y / gcd(x,y));

傳值呼叫與傳址呼叫之比較範例 10 傳值呼叫參數值 10 &num_x &num_y 傳址呼叫參數值 /* example:傳值呼叫和傳址呼叫 (交換XY)*/ /* --------------------Call by value---------- */ void swap1(int x,int y) { int temp; temp = x; x = y; y = temp; } /* ------------------- Call by address ------ */ void swap2(int *x,int *y) temp = *x; *x = *y; *y = temp; 續下頁 10 num_x num_y 複製值 x y 傳值呼叫參數值 X 與 y 兩數 交換不影響num_x與num_y 10 &num_x &num_y num_x num_y 指向 x y 傳址呼叫參數值 Call_v_a.c 傳值是將引數的值複製給函式的參數, 傳址則複製引數值的地址值 若呼叫函式的引數, 本身為指標變數時, 則直接將引數內所儲存的位址值複製給參數, 而不需使用[&] 取得位址 傳值呼叫時, 函式的動作不會影響主程式所傳數引數的變數值 傳址呼叫時, 因引數與函式內參數均透過位址值存取同一塊記憶體, 因此, 於函式中更改參數值時, 主程式中所傳入 的引數, 也會同時更改 X 與 y 兩數 交換後, 則分別指向num_y 與 num_x

傳值呼叫與傳址呼叫之比較範例 – 續 接上頁 /* ---------------------Main function---------------------- */ void swap1(int, int); void swap2(int *, int *); void main() { int num_x = 10, num_y=0; printf(" X Y \n"); printf(" Initial value %d %d \n",num_x, num_y); swap1(num_x, num_y); /* call by value */ printf(" Call by value %d %d \n",num_x, num_y); swap2(&num_x, &num_y); /* call by address */ printf(" Call by address %d %d \n",num_x, num_y); } 結果 X Y Initial value 10 0 Call by value 10 0 Call by address 0 10

陣列資料傳遞

回顧-Example 1a 範例:呼叫函式,印出學號 #include<stdio.h> void p(char sn[ ]); /*宣告函式,函式原型void是不傳回值,傳入一個char型態*/ int main(void) /*主程式*/ { char num[10]; printf("請輸入學號:"); scanf("%s",&num); p(num); /*呼叫p函式*/ printf("\n"); system("pause"); } void p(char sn[ ]) /*p函式*/ printf("\n\n您的學號為 %s",sn); 進入副程式 離開副程式

回顧-Example 1b 範例:呼叫函式,印出學號 #include<stdio.h> void p(char *); /*宣告函式,函式原型void是不傳回值,傳入一個char型態*/ int main(void) /*主程式*/ { char num[10]; printf("請輸入學號:"); scanf("%s",&num); p(num); /*呼叫p函式*/ printf("\n"); system("pause"); } void p(char num[ ]) printf("\n\n您的學號為 %s",num); /*p函式的num[ ]與主程式裡的num[ ]是不同地*/

陣列資料傳遞 – 範例一 利用函數方式,將陣列中的最小值傳回 #include <stdio.h> int minimum(int *, int); void main() { int array[5]; int minimum_value; int i; for( i =0; i<5; i++) printf("\1: input value %d ==> ", i+1); scanf("%d", &array[i]); } minimum_value = minimum(array, 5); printf("\2: The minimum value is %d \n", minimum_value); 續下頁 f_array1.c : input value 1 ==> 5 : input value 2 ==> 1 : input value 3 ==> 3 : input value 4 ==> 2 : input value 5 ==> 6 : The minimum value is 1

陣列資料傳遞 – 範例一續 接上頁 int minimum(int *var, int size) { int min, i; min = var[0]; for( i=0; i<=size-1; i++) if(var[i] < min) min = var[i]; return min; }

陣列資料傳遞 – 範例二 (共2頁) 顯示二維陣列內所有元素的值 #include <stdio.h> #include <stdlib.h> /* 顯示大小為rol,col的二維陣列所有元素的值 */ void prn_arr(int * arr,int rol,int col); int main(void)/* 主程式開始 */ { int array[][3]={{1,2,3},{4,5,6}}; prn_arr(array[0],2,3);/* 顯示array所有的元素 */ system("PAUSE");/* 暫停程式執行 */ return 0;/* <-程式正常結束,傳回0 */ } 續下頁 f_array2.c array[0][0]= 1 array[0][1]= 2 array[0][2]= 3 array[1][0]= 4 array[1][1]= 5 array[1][2]= 6 Press any key to continue . . .

陣列資料傳遞 – 範例二 (續) 接上頁 /* prn_arr 函數的宣告 作用:顯示大小為rol,col的二維陣列所有元素的值 void prn_arr(int *arr,int rol,int col) { int i,j; /* 顯示陣列元素值 */ for(i=0 ; i<rol ; i++) for(j=0 ; j<col ; j++) printf("array[%d][%d]= %d\n",i,j,arr[i*col+j]); } //另一種寫法,由使用者輸入陣列值 #include <stdio.h> /* 顯示大小為rol,col的二維陣列所有元素的值 */ void prn_arr(int * arr,int rol,int col); int main(void)/* 主程式開始 */ { int array[2][3]=; prn_arr(array[0],2,3);/* 顯示array所有的元素 */ system("PAUSE");/* 暫停程式執行 */ return 0;/* <-程式正常結束,傳回0 */ } void prn_arr(int *arr,int rol,int col) int i,j,k; for(i=0 ; i<rol ; i++) for(j=0 ; j<col ; j++) printf(“Please input six number:”); scanf(“%d”,&k); /*抓二維陣列的值*/ printf(“array[%d][%d]= %d\n”,i,j,*(arr+j)=k); /* *(arr+j)代表從arr[0][0]開始, 將k(也就是將從營幕上使用者輸入的值)指派給arr陣列 */