C 编程中函数的不同类别是什么?
根据是否存在参数以及是否返回值,函数分为以下类别:
无参数无返回值函数
示例
#include<stdio.h> main (){ void sum (); clrscr (); sum (); getch (); } void sum (){ int a,b,c; printf("enter 2 numbers:
"); scanf ("%d%d", &a, &b); c = a+b; printf("sum = %d",c); }
输出
Enter 2 numbers: 3 5 Sum=8
无参数有返回值函数
示例
#include<stdio.h> main (){ int sum (); int c; c= sum (); printf(“sum = %d”,c); getch (); } int sum (){ int a,b,c; printf(“enter 2 numbers”); scanf (“%d%d”, &a, &b); c = a+b; return c; }
输出
Enter two numbers 10 20 30
有参数无返回值函数
示例
#include<stdio.h> main (){ void sum (int, int ); int a,b; printf("enter 2 numbers"); scanf("%d%d", &a,&b); sum (a,b); getch (); } void sum ( int a, int b){ int c; c= a+b; printf (“sum=%d”, c); }
输出
Enter two numbers 10 20 Sum=30
有参数有返回值函数
示例
#include<stdio.h> 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 two numbers 10 20 Sum=30
广告