C语言中函数间如何建立通信?
函数之间通过参数和返回值进行通信。
C函数的形式如下:
return-datatype function name (argument list){
local variable declarations;
executable statements(s);
return (expression);
}例如,void mul (int x, int y)
{
int p;
p=x*y;
printf("product = %d”,p);
}返回值及其类型
- 函数可以向调用函数返回一个值,也可以不返回。
- 这将通过使用return语句完成。
- 返回值类型为void、int、float、char和double。
- 如果函数不返回任何值,则其返回类型为'void'。
函数名
函数必须遵循与C语言中变量名相同的规则。
函数名不能是预定义的函数名。
参数列表
在这个列表中,变量名用逗号隔开。
参数变量从调用函数接收值,这为从调用函数到被调用函数的数据通信提供了一种手段。
调用函数
可以使用语句中的函数名来调用函数。
函数定义
每当调用函数时,控制权就会转移到函数定义。
被调用函数中的所有语句都称为函数定义。
函数头
- 函数定义中的第一行。
实际参数
- 函数调用内部的所有变量。
形式参数
函数头内部的所有变量都称为形式参数。
示例
以下是C程序中函数间通信的示例:
#include<stdio.h>
#include<conio.h>
main ( ){
int mul (int, int); // function prototype
int a,b,c;
clrscr( );
printf ("enter 2 numbers”);
scanf("%d %d”, &a, &b);
c = mul (a,b); // function call
printf("product =%d”,c); Actual parameters
getch ( );
}
int mul (int a, int b){ // Formal parameters
//function header
int c;
c = a *b; //Function definition
return c;
}输出
执行上述程序时,会产生以下结果:
Enter 2 numbers: 10 20 Product = 200
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP