先動手編寫程序:
#include <stdio.h>int add(int x, int y){ int z = x + y; return z;}int main(){ int a = 1; int b = 2; int c = add(a, b); printf("c = %d/n", c); return 0;}運行結果:
c = 3
程序分析:
(1)
函數(shù)定義的形式為:
類型 函數(shù)名稱(類型 形式參數(shù),……)
{
函數(shù)體
}
(2)
對應于咱們的程序,定義了一個名為add的函數(shù),add左邊的int代表的函數(shù)的返回值的類型。對應于函數(shù)體中返回的z的類型。
(3)
x和y是形式參數(shù),main()函數(shù)中的add(a,b)中a和b是實際參數(shù)。
(4)
main函數(shù)中的add(a,b)后面沒跟大括號,說明是函數(shù)調用,不是函數(shù)定義。具體的add函數(shù)定義在main函數(shù)的上方。
(5)
調用add函數(shù)時,實際參數(shù)(實參)傳遞給形式參數(shù)(形參),這樣x = a = 1, y = b = 2
(6)
z = x + y = 1 + 2 = 3
(7)
int c = add(a, b)說明要把add函數(shù)得到的值賦給c。即c = add(1, 2) = z = 3。最終c = 3
新聞熱點
疑難解答