基本语法 返回类型 函数名 (形参列表){ 执行语句...;//函数体 return 返回值;//可选 }
快速入门案例 使用函数来解决计算问题
#Include<stdio.h>
//说明
//1. 函数名 cal
//2. 有返回值 double
//3. 形参列表为 (int n1,int n2,char oper)
//4. 在函数中,我们使用的变量名需要和形参列表的变量名一样
double cal(int n1,int n2,char oper) {
//定义一个变量res,保存运算的结果
double res = 0.0;
switch(oper) {
case '+':
res = n1 + n2;
break;
case '-':
res = n1 - n2;
break;
case '*':
res = n1 * n2;
break;
case '/':
res = n1 / n2;
break;
default:
printf("你的运算符号有误");
}
printf("\n%d %c %d = %.2f\n",n1,oper,n2,res);
return res;
}
void main(){
int num1 = 10;//第一个数
int num2 = 20;//第二个数
double res = 0.0;//结果
char oper = '-';//运算符
//我们又要接收两个数和一个运算符
int num3 = 60;
int num4 = 80;
char oper2 = '+';
double res2 = 0.0;
printf("使用函数解决计算任务~~");
res = cal(num1,num2,oper);
printf("\n cal函数返回的的结果是 res = %.2f",res);
printf("使用函数解决第二个计算任务~~");
res2 = cal(num33,num4,oper2);
printf("\n cal函数返回的的结果是 res2 = %.2f",res2);
}
头文件快速入门 C程序相互调用函数,我们将cal 声明到文件 myfun.h,在myfun.c中定义cal 函数,当其他文件需要使用到myfun.h 声明的函数时,可以#include 该头文件,就可以使用了
myfun.h
#include<stdio.h>
//声明函数
int myCal(int n1,int n2,char oper);
myfun.c
#include<stdio.h>
int myCal(int n1,int n2,char oper){
//定义一个变量 res,保存运算的结果
double res = 0.0;
switch(oper2){
case'+':
res2 = num3 + num4;
break;
case'-':
res2 = num3 - num4;
break;
case'*':
res2 = num3 * num4;
break;
case'/':
res2 = num3 / num4;
break;
default:
printf("你的运算符号有误");
}
printf("%d %c %d = %.2f\n",num3,oper2,num4,res2);
return res;
}
hello.c
#include<stdio.h>
//引入需要的头文件
#include"myfun.h"
void main(){
//使用 myCal 完成计算任务
int n1 = 10;
int n2 = 50;
char oper = '-';
double res = 0.0;
//调用 myfun.c 中定义的函数 myCal
res = myCal(n1,n2,oper);
printf("\nres=%.2f",res);
getchar();
}
调用规则(适用于java,c++,php)
一个函数在函数体内又调用了本身,称其为递归调用
递归课堂练习题
斐波那契数
#include<stdio.h>
int fbn(int n){
if(n==1 || n==2){
return 1;
}else{
return fbn(n-2)+fbn(n-1);
}
}
int main(){
int n = 8;
int res = fbn(n);
return res;
}
求函数值 已知 f(1)=3;f(n)=2*f(n-1)+1; 求f(n)
#include<stdio.h>
int f(int n){
if(n==1){
return 3;
}else{
return 2*f(n-1)-1;
}
}
int main(){
int res = f(5);
return res;
}
猴子吃桃,每天吃一半加一个,第十天还没吃发现只剩一个
#include<stdio.h>
int peach(int day){ if(day==10){ return 1; }else{ return (peach(day+1)+1)*2; } }
void main(){ int day = 1; int res = peach(day); printf("第%d天有%d个桃子",day,res); getchar(); }
函数注意事项和细节讨论
函数的形参列表可以是多个
C语言传递参数可以是值传递,也可以指针传递,又叫引用传递
函数的命名遵循标识符命名规范,首字母不能是数字,可以采用 驼峰法 或者 下划线法 getSum 或者 get_sum
函数中的变量是局部的,函数外不生效
基本数据类型默认是值传递的,即进行值拷贝。在函数内修改,不会影响到原来的值
void f2(int n) {
n++;
printf("\nf2中的n=%d",n);//n=10
}
void main() {
//函数中的变量是局部的,函数外不生效
int n = 9;//n=9
f2(n);
printf("\nmain中的n=%d",n);
getchar();
}
如果希望函数内的变量能修改函数外的变量,可以传入变量的地址&,函数内以指针的方式操作变量。从效果上看类似引用(即传递指针)
void f3(int* p){
(*p)++;//此处的修改会对函数外的变量有影响
}
void main() {
//函数中的变量是局部的,函数外不生效
int n = 9;
f3(&n);
printf("\nmain 函数中 n=%d",n);
getchar();
}
C语言不支持函数重载
C语言支持可变参数函数//知道即可
编写一个函数 swap(int* n1,int* n2)可以交换n1 和 n2 的值
#include<stdio.h>
//说明
//1. 函数名是 swap
//2. 形参是两个指针类型 int*
void swap(int* n1,int* n2){
int temp = *n1;
*n1 = *n2;
*n2 = temp;
}
void main() {
int n1 = 1;
int n2 = 2;
swap(&n1,&n2);
printf("%d %d",n1,n2);
getchar();
}
值传递,指针传递(引用传递)
值传递:变量直接存储值,内存通常在栈中分配 默认是值传递的数据类型:基本数据类型,结构体,共用体,枚举类型
引用传递:变量存储的是一个地址,这个地址对应的空间才是真正存储数据(值) 默认是引用传递的数据类型:指针和数组
如果希望函数内的变量能够修改函数外的变量,可以传入变量的地址&,函数内以指针的方式操作变量(*指针)
所谓变量作用域(Scope),就是指变量的有效范围
函数内部声明/定义的局部变量,作用域仅限于函数内部
void sayHello() {
char name[] = "tom";
printf("hello %s \n",name);
}
void main() {
sayHello();
//这里不能使用到sayHello 的 name 变量
printf("name=%s",name);//这里将提示,没有定义name
}
函数的参数,形式参数,被当做该函数内的局部变量,如果与全局变量同名,它们会优先使用局部变量(编译器使用就近原则)
int n = 20;//函数外部定义的变量,就是全局变量
//函数形参,会被视为 f10 的局部变量
//说明:在局部变量和全局变量同名是,以局部变量为准(就近原则)
void f10(int n) {
printf("\nn=%d",n);
}
void main() {
f10(10);//输出就是10
}
在一个代码块,比如 for/if 中的局部变量,那么这个变量的作用域就在该代码块
void main() {
int i = 0;
for(i = 0;i<10;i++){
int k = 90;//k的作用域在for代码块中
printf("i=%d k=%d\n",i,k);
}
printf("k=%d",k);
getchar();
}
在所有函数外部定义的变量教全局变量,作用域在整个程序有效
课堂练习题 下面的代码输出什么内容
#include<stdio.h>
double price = 200.0;//全局变量
void test01(){
printf("%.2f\n",price);//就是 全局变量:200.0
}
void test02(){//采用就近原则
double price= 250.0;//如果这里没有double,全局变量就会被修改,影响到后面程序运行的结果
printf("%.2f\n",price");
}
void main(){
printf("main price=%.2f\n",price);//200.00
test01();//200.00
test02();//250.00
test01();//200.00
getchar();
}
#include<stdio.h>
int n =10;
void func1() {
int n = 20;
printf("func1 n:%d\n",n);
}
void func2(int n) {
printf("func2 n:%d\n",n);
}
void func3(int n) {
printf("func3 n:%d\n",n);
}
int main() {
int n = 30;
func1();
func2(n);
func3();
{
int n = 40;
printf("block n:%d\n",n);
}
printf("main n:%d\n",n);
getchar();
return 0;
}
本文章使用limfx的vscode插件快速发布