C++语言中关于函数参数传递方式的探讨

2012-04-29 22:54:41孙勤
电脑知识与技术 2012年21期

孙勤

摘要:在C+ +程序设计函数调用过程中,函数参数的传递是至关重要的内容,参数传递的方式不同,其传递的内容也不同类型,在程序设计过程中,选择恰当的参数传递方式,不仅可以使程序更加简洁,同时可以提供程序的运行效率,该文较为全面的论述了C++中函数参数传递的几种常见方式以及各种方式的优点及区别。

关键词:C++;函数调用;参数传递

中图分类号:TP311文献标识码:A文章编号:1009-3044(2012)21-5116-03

Discussion on the Function Parameter Passing in C++ Programming

SUN Qin

(Gansu Agricultural University, Lanzhou 730070, China))

Abstract: In the process of function call of C+ + programming, the function parameter passing is of the utmost importance. When the methods of parameter passing are different, the contents passed are also of different styles. In the programming, selecting proper methods of parameter passing can not only simplify the program, but also can promote the operation efficiency of the program. This paper gives a com prehensive discussion about several common methods of the function parameter passing in C+ + as well as their advantages and their differ ences.

Key words: C++; function call; function parameter passing

C++语言是在C语言的基础上扩充了面向对象的概念和相应的处理机制而形成的一种混合型的程序设计语言,C++全面兼容C。在一个C/C++程序中,为避免主函数庞杂、头绪不清,阅读和维护困难,经常用模块化程序设计的思路,采用“组装”的办法简化程序设计的过程,即事先编好一批实现各种不同功能的函数,把它们保存在函数库中,需要时直接用。C/C++程序可由一个主函数和若干个其他函数构成,主函数调用其他函数,其他函数也可以互相调用。在程序设计中善于利用函数,可以减少重复编写程序段的工作量,同时可以方便地实现模块化的程序设计。

1 C++中的函数

从用户使用的角度看,函数有两种,一是库函数,它是由系统提供的;二是用户自己定义的函数,它是用以解决用户专门需要的函数。

从函数的形式看,函数分两类。

1)无参函数。无参函数一般用来执行一组特定的操作,一般不带回函数值。

2)有参函数。在调用函数时,主调函数通过参数向被调用函数传递数据,一般情况下,被调用函数调用结束时会向主调函数返回一个函数值供主调函数使用。……

登录APP查看全文