1、函数的定义和调用
示例代码如下:
#include <stdio.h>
void main(){
int twoSum(int,int); // 函数调用声明
int c;
c = twoSum(1,2); // 调用函数
printf("%d\n",c); // 打印函数返回值
}
int twoSum(int a,int b){
return a + b;
}
2、局部变量和全局变量
局部变量:在函数内部定义的变量,只在函数内部有效。
全局变量:在函数外部定义的变量,在定义位置到整个程序结束有效。
示例代码如下:
#include <stdio.h>
int max; // 全局变量
void main(){
int twoSum(int,int); // 函数调用声明
twoSum(1,2); // 调用函数
printf("%d\n",max); // 打印两个数中的最大值
}
int twoSum(int a,int b){
if(a > b){
return max = a; // 把局部变量a的值赋值给全局变量
}else{
return max = b;
}
}