我们先写一个函数
#include <stdio. h>
int max(int a, int b)
{
if (a>b){
return a;
}else{
return b;
}
}
上面的函数各部分的意思为:
返回值类型 函数名(参数)
{
函数体
}
如果返回值类型为void 代表没有返回值
详见:
函数返回值类型为void是什么意思
函数调用
#include <stdio. h>
int max(int a, int b)
{
if (a>b){
return a;
}else{
return b;
}
}
int main()
{
int c;
c = max(10,12);
return c;
}
函数调用方法:
函数名(参数)
例如:
max(10,12)
在上面的函数调用例子中,我们把求最大值函数max()函数写到了main()函数的上边,这是因为编译器是从上往下编译的,它需要先知道max()函数长什么样子,才能进行调用。
如果不想把求最大值函数max()函数写到了main()函数的上边,可以采用函数原型声明
#include <stdio. h>
int max(int a, int b); //函数原型声明
int main()
{
int c;
c = max(10,12);
return c;
}
int max(int a, int b) //函数定义
{
if (a>b){
return a;
}else{
return b;
}
}
在上面的例子中采用了函数原型声明