C 编程中有哪些不同类型的函数?
功能大致分为两种类型,如下所示-
预定义功能
用户定义函数
预定义(或)库函数
这些函数已经在系统库中定义。
程序员可以重用系统库中的现有代码,这有助于编写无错代码。
用户必须了解函数的语法。
例如,sqrt()函数在math.h库中可用,其用法为y=sqrt(x),其中x=number必须为正数。
如果x值为25,即y=sqrt(25),则'y'=5。
同理,printf()在stdio.h库clrscr()中可用,在conio.h库中可用。
程序
#include输出结果#include #include main (){ int x,y; clrscr (); printf (“enter a positive number”); scanf (“ %d”, &x) y = sqrt(x); printf(“squareroot = %d”, y); getch(); }
Enter a positive number 25 Squareroot = 5
用户定义函数
这些功能必须由程序员或用户定义。
程序员必须为这些函数编写代码并在使用它们之前正确测试它们。
该函数的语法由用户给出,因此无需包含任何头文件。
例如,main()、swap()、sum()等是一些用户定义的函数。
示例
#include输出结果#include main (){ int sum (int, int); int a, b, c; printf (“enter 2 numbers”); scanf (“ %d %d”, &a ,&b) c = sum (a,b); printf(“sum = %d”, c); getch(); } int sum (int a, int b){ int c; c=a+b; return c; }
Enter 2 numbers 10 20 Sum = 30
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下