C++函数

一:函数
1.C++拥有函数(function)和类(class)这样两种模块,C++的应用程序大多是由各种标准库提供的模块和程序员自定义的模块组合而成,其中函数可以被多次调用,使C++中程序中的函数被重复使用;
2.函数模块包括函数定义,函数原型说明和函数调用三方面内容:函数定义用来指明函数的原型和函数体,一个函数只能定义一次;函数原型说明用来指出函数的原型;函数调用就是执行函数,完成函数提供的功能。
2.1-函数必须先定义后使用,函数定义就是编写实现函数功能的程序段。函数定义由两部分组成:一部分是函数首部,其中包括函数值类型,函数名,函数的参数和返回类型,另一部分是函数操作的功能描述,是由函数体内的各语句序列构成;
2.2-函数原型位于程序的开始处,函数原型说明的目的是让编译程序能够找到要使用的函数;
函数原型说明的格式是:
函数值类型 函数名(参数列表);
2.3-在任何情况下,C++能自动将变量的类型转换为与参数一致的类型;
例:参数自动转换:
#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
int dec(int a,int b);
int main()
{
int li_num;
li_num=dec(10.3,3.0);
cout<<“Number=”<<li_num;
return 1;
}
int dec(int a,int b)
{
return a-b;
}


程序输出结果为:
Number=7

3.若在定义函数时没有给出函数返回值类型,则默认为int型;
例:使用标准函数abs();
#include <iostream.h>
#include<stdlib.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

int main()
{
int i;
i=abs(-10);
cout<<i<<"\n";
cout<<abs(-20);
abs(12);
return 0;
}


程序输出结果为:
10
20

4.返回指针:
4.1-函数的返回值是一个指针,这种函数称为指针型函数;
4.2-指针型函数的 定义形式为:
类型 *函数名(形式参数列表)
{}
4.3-指针型函数的原型说明形式为:
类型 函数名(参数列表);
#include <iostream.h>
using namespace std;
/
run this program using the console pauser or add your own getch, system(“pause”) or input loop */
char *match();
int main()
{
char *p;
p=match();
if§cout<<“the char of match() function is”<<*p<<endl;
else cout<<“no!\n”;
return 0;
}
char *match()
{
char a=‘B’;
return &a;
}


the char of match() function isB


Process exited after 0.1155 seconds with return value 0
请按任意键继续…

5.函数的参数:
5.1-C++采用三种方法向函数传递参数:传值,传地址和传引用。对应的实际参数可以是常量,变量,数组元素和表达式;
a.传值方式可以防止被调用函数改变参数的原始值;
b.传地址调用函数:函数的实参是数据的地址,形参是地址(指针)变量;这种调用方式向函数传递的是地址值,形参的任何改变都将导致实参的改变;

例:
#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
void swap(int *a,int *b);
int main()
{
int a,b;
a=1;
b=2;
swap(&a,&b);
cout<<“main program a=”<<a<<"\n";
cout<<“main program b=”<<b<<"\n";
return 0;
}
void swap(int *a,int *b)
{
int x;
cout<<“function swap begin *a=”<<*a<<"\n";
cout<<“function swap begin *b=”<<*b<<"\n";
x=*a;
*a=*b;
*b=x;
cout<<“function swap begin *a=”<<*a<<"\n";
cout<<“function swap begin *b=”<<*b<<"\n";
}


function swap begin *a=1
function swap begin *b=2
function swap begin *a=2
function swap begin *b=1
main program a=2
main program b=1


Process exited after 0.01441 seconds with return value 0
请按任意键继续. . .

c.传引用调用函数:用引用(变量的别名)作函数形参,用与引用对应的变量作实参。引用实际上也是使用数据的地址;

例:
#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
void reciprocal(double &n);
int main()
{
double val=10.0;
cout<<“original value of val is”<<val<<"\n";
reciprocal(val);
cout<<“reciprocal value of val is”<<val<<"\n";
return 0;
}
void reciprocal(double &n)//形参列表中使用了&n,表明n是一个引用参数;
{
n=1.0/n;
}


original value of val is10
reciprocal value of val is0.1


Process exited after 0.01463 seconds with return value 0
请按任意键继续. . .

5.2-用数组名做函数实际参数:

例:(尽管函数形参的定义形式不同,但作用相同,都产生一个指向数组的指针参数)
#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
void a1(int num[5]);
void a2(int num[]);
void a3(int *num);
int main()
{
int count[5]={1,2,3,4,5};
a1(count);
a2(count);
a3(count);
return 0;
}
void a1(int num[5])
{
int i;
cout<<“function a1:”;
for(i=0;i<5;i++)
cout<<num[i]<<"";
cout<<"\n";
}
void a2(int num[])
{
int i;
cout<<“function a2:”;
for(i=0;i<5;i++)
cout<<num[i]<<"";
cout<<"\n";
}
void a3(int *num)
{
int i;
cout<<“function a3:”;
for(i=0;i<5;i++)
cout<<num[i]<<"";
cout<<"\n";
}


function a1:12345
function a2:12345
function a3:12345


Process exited after 0.01572 seconds with return value 0
请按任意键继续. . .

5.3-函数main()的参数:
a.c++程序的main()函数可以有两个命令行参数,分别是argc和argv,这两个参数是可选项,当不需要命令行行参数时可省略他们,通过命令行参数可以向main()函数传递信息,达到控制程序的目的;
定义形式为:
int main(int argc, char* argv[])
{
return 0;
}
b.参数argc用于存放命令行参数的数量,是一个整型数,由于程序名被当做第一个参数,所以argc的值至少为1;
c.参数argv是一个字符串指针数组。argv右侧的方括号【】说明该数组是不定长数组,所有命令行参数都被当做字符串传给main()函数,参数argv通过下标接收不同的字符串。例如用argv【0】指向程序名,用argv【1】指向程序名后的第一个参数;
例:
#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

int main(int argc, char** argv)
{
int i;
for(i=1;i<argc;i++)
cout<<argv[i]<<" ";
return 0;
}

注:假设程序的可执行文件名为test.exe,在DOS操作系统下,若输入命令test this is a test,则程序输出结果是:this is a test
运行该程序,当输入命令行为test this is a test时,argc=5;argv[0]=“test”,argv[1]=“this”,argv[2]=“is”,argv[3]=“a”,argv[4]=“test”;
参数argc和argv是任意的,可以随自己的意愿随意定义参数名,argc和argv不过是一种习惯的写法;
例2:

#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

int main(int argc, char** argv)
{
if(argc!=2)
cout<<“you forgot to input parameter\n”;
cout<<“hello”<<argv[1]<<"\n";
cout<<argv[0]<<“normal end\n”;
return 0;
}
若该程序的可执行文件名为test.exe,在DOS操作系统下,若输入命令行test abc,则程序输出结果为:
hello abc
test.exe normal end
若只输入程序名test,则程序输出结果为:
you forgot to input parameter

6.局部变量和全局变量
6.1-作用域是数据的适用范围。变量的作用域是程序中的的一段区域,在这个区域中的变量是可见的。按作用域大小可以把变量分为局部变量和全局变量。
6.2-在函数或者类内说明的变量是局部变量。局部变量的作用域也称为块作用域。函数内部使用的局部变量包括形式参数和函数体中定义的变量。
6.3-全局变量是在函数外或类外定义的变量。应尽量减少全局变量,如果全局变量与函数的局部变量同名,则在该作用域中全局变量无效,为了在函数内使用与局部变量同名的全局变量,应在全局变量前加上作用域分辨符“::”。

7.递归函数
7.1-递归是一种用自己定义自己的进程。如果一个函数在被调用的过程中,又直接或间接的调用本身,这个过程称为函数的递归调用,在函数的函数体中如果有调用该函数的语句,这个函数称为递归函数。注意递归是个循环过程,编写递归函数时必须有能够终止循环,跳出递归调用的功能;
7.2-编写递归函数用于解决递归问题,如果解决某问题依赖于下一问题的解决,那么这个问题属于递归问题。如计算某数的阶乘;

例:用递归函数求整数阶乘;
#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop /
long f(int n);
int main()
{
cout<<“1的阶乘是”<<f(1)<<endl;
cout<<“4的阶乘是”<<f(4)<<endl;
cout<<“6的阶乘是”<<f(6)<<endl;
return 0;
}
long f(int n)
{
if(n==1) return 1;
else return n
f(n-1);
}


1的阶乘是1
4的阶乘是24
6的阶乘是720


Process exited after 0.1203 seconds with return value 0
请按任意键继续. . .

8.函数指针
8.1-可以定义一个指针变量存放函数名,即存放函数的入口地址,存放函数的入口地址的变量称为函数指针变量,函数指针变量的说明形式为:
类型(*函数名)(参数列表);
int (*p)(int int);

例:用函数指针调用函数;

#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
int f1(int a,int b);
int main()
{
int (*p)(int,int);
int result;
p=f1;
result=(*p)(10,20);
cout<<result;
return 0;
}
int f1(int a,int b)
{
return a+b;
}


30
Process exited after 0.116 seconds with return value 0
请按任意键继续. . .

9.库函数
9.1-常用的头文件:
bios.h ----- BIOS中断
complex.h-----C++的复数
conio.h-----操作台和I/O端口
ctype.h-----字符函数
dos.h-----DOS中断
graphics.h-----图形历程
io.h-----文件处理和低级I/O
iostream.h-----C++的流例程
math.h-----数字函数
stdio.h-----C的流例程
stdlib.h-----标准库例程
string.h-----串函数
time.h-----日期和时间例程
9.2-常用的C++算数运算函数
double acos(double x)-----反余弦
double asin(double x)-----反正弦
double atan(double x)-----反正切
double atan2(double x,double y)-----反正切
double ceil(double x)-----最大整数
double cos(double x)-----余弦
double cosh(double x)-----双曲余弦
double exp(double x)-----自然数e的指数(e的x次方)
double fabs(double x)-----绝对值
double floor(double x)-----最小整数
double fmod(double x,double y)-----模数操作
double frexp(double x,double *esponeat)-----分成尾数和指数
double hypot(double x,double y)-----斜边
double ldexp(double x,int esponeat)-----x乘以2的指数幂
double log(double x)-----自然对数
double log 10(double x)-----常用对数
double modf(double x,double *ipart)-----尾数和指数
double poly(double x,int degree,double coeffs[])-----多项式
double pow(double x,double y)-----x的y次幂

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值