- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//函数原型
void main()
{
int sum,x,y;
cout<<"请输入被加数和加数:"<<endl;
cin>>x>>y;
sum=add(x,y);
//函数调用
cout<<"Sum="<<sum<<endl;
}
int add(int a,int b) {
return a+b; }
//函数定义运行结果: 请输入被加数和加数: 213 625 Sum=838
3.1 函数的定义与申明
2、函数的申明
函数声明的语法形式
类型标识符 函数名(形式参数表) {
语句序列 }
是被初始化的内部 变量,寿命和可见 性仅限于函数内部
若无返回值,写void
形式参数表 <type1> name1, <type2> name2, ..., <typen> namen
函数的返回值 由 return 语句给出;无返回值的函数(void类型), 不必写return语句
3.2 函数的调用与参数传递
2、函数调用时的参数传递
引用传递
数据类型& 引用名=数据类型变量名;
引用(&)是标识符的别名;
【例3-6】 引用的建立和使用。
#include <iostream>
cout<<refa<<“ ”<<a<<endl;
using namespace std;
cout<<refb<<" "<<b<<endl;
z=max(x,y);
cout<<"maxmum= "<<z<<endl;
}
int max(int a,int b)
//函数定义
{
if(a>b)return a;
else return b;
}
【例3-2】 实现两个数相加
#include <iostream.h>
int add(int ,int);
【例3-3】 按值传递
#include <iostream.h> void swap(int,int); void main() {
int a=3,b=4; cout<<"a="<<a<<",b=“
<<b<<endl; swap(a,b); cout<<"a="<<a<<",b="
<<b <<endl; } void swap(int x,int y) { int t=x; x=y; y=t; }
【例3-14】 求三个操作数之和
#include <iostream.h>
int sum(int,int,int);
#include <string.h>
int up_letter(char ch);
void main( )
{ char str[80]; int i;
cout<<"please input a string :";
cin>>str;
for(i=0;i<strlen(str);i++)
{ if (up_letter(str[i])) str[i]-=32; }
第三章 函数
青岛理工大学
本章主要内容
3.1 函数的定义和声明 3.2 函数的调用与参数传递 3.3 内联函数 3.4 函数重载 3.5 带默认形参值的函数 3.6 作用域与生存周期 3.7 多文件结构
3.1 函数的定义与申明
1、函数的定义
无参函数的一般形式 有参函数的一般形式
类型说明符 函数名() {
30 30
30 30
【例3-7】 引用传递
#include "iostream.h"
void swap(int &,int &);
void main()
{
int a=3,b=4;
cout<<“a=”<<a<<“,b=”<<b<<endl;
swap(a,b);
cout<<“a=”<<a<<“,b=”<<b<<endl;
int a=3,b=4; cout<<"a="<<a<<",b=“
<<b<<endl; swap(&a,&b); cout<<"a="<<a<<",b=“
<<b<<endl; }
void swap(int *x,int *y) {
int t=*x; *x=*y; *y=t; }
运行结果: a=3,b=4 a=4,b=3
cout<<"the result is :"<<str<<endl;
}
int up_letter(char ch)
{ if (ch>='a'&&ch<='z')
return 1; else
运行结果:
return 0;
Please input a string:goodMORNING3456
}
The result is:GOODMORNING3456
类型说明 语句
}
类型说明符 函数名(形式参数表) 形式参数类型说明
{ 类型说明 语句
}
【例3-1】 max函数的位置示例
#include <iostream.h> int max(int a,int b) {
if(a>b)return a; else return b; } void main() { int max(int a,int b); int x,y,z; cout<<"input two numbers:"<<endl); cin>>x>>y; z=max(x,y); cout<<"maxmum= "<<z<<endl; }
【例3-12】将字符数组str1中所有小写字母('a'~'z')转换成大写字母
#include <iostream.h> #include <str(char ch); void main( ) { char str[80]; int i;
cout<<"please input a string :"; cin>>str; for(i=0;i<strlen(str);i++)
}
void swap(int &x,int &y)
{ int t=x; x=y; y=t;
运行结果: a=3,b=4 a=4,b=3
}
3.2 函数的调用与参数传递
3、函数的嵌套调用和递归调用
函数的嵌套调用
【例3-8】函数的嵌套调用, 求三个数 中最大数和最小数的差值。
#include<iostream> int dif(int x,int y,int z) using namespace std; { int max(int x,int y,int z) return max(x,y,z)-min(x,y,z);
int main() {
int a=30,b=20;
refa=b; //此时引用refa仍旧是a的 别名,只是把b的值给到a。
cout<<refa<<“ ”<<a<<endl;
int &refa=a,&refb=b; refa=a+20; b=refb+10;
return 0; 运行结果:
}
50 50
{ if (up_letter(str[i])) str[i]-=32; } cout<<"the result is :"<<str<<endl; } inline int up_letter(char ch) { if (ch>='a'&&ch<='z') return 1; else return 0; }
if (n= =1|| n= =0) total=1; 12! is 479001600
else total= fac(n-1)* n;
return total;
}
void main( )
{ int n;
cout<<"please input a integer :";
cin>>n;
cout<<n<<"! is "<<fac(n)<<endl;
函数类型 递归函数名f (参数x ) {