[C++] 用Xcode来写C++程序[4] 函数
用Xcode来写C++程序[4] 函数
此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值.
引用函数:防止复制对象,减少系统开销
内联函数:编译的时候根据具体情形将代码嵌入进去,成不成功编译器说了算,减少系统开销提升性能
引用函数(防止篡改初始值的入参声明方式):防止修改数据源
函数参数带有默认值:函数的某个参数可以给定默认值,精简函数的使用
最简单的函数
#include <iostream>
using namespace std; int addition (int a, int b) {
return a + b;
} int main () {
int z;
z = addition (,);
cout << "The result is " << z << endl;
}
打印结果
The result is
Program ended with exit code:
传递引用(int& 表示)
#include <iostream>
using namespace std; void duplicate (int& a, int& b, int& c) {
a *= ;
b *= ;
c *= ;
} int main () {
int x = , y = , z = ;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z << endl; return ;
}
打印结果
x=, y=, z=
Program ended with exit code:
防止篡改数据源(const 修饰变量)
#include <iostream>
#include <string>
using namespace std; string concatenate (const string& a, const string& b) {
return a + b;
} int main () { string x = "You";
string y = "XianMing"; cout << concatenate(x, y) << endl; return ;
}
打印结果
YouXianMing
Program ended with exit code:
内联函数(减少函数调用开销)
#include <iostream>
#include <string>
using namespace std; inline string concatenate (const string& a, const string& b) {
return a + b;
} int main () { string x = "You";
string y = "XianMing"; cout << concatenate(x, y) << endl; return ;
}
打印结果
YouXianMing
Program ended with exit code:
带默认值的函数(如果不赋值,则有一个默认值)
#include <iostream>
using namespace std; int divide (int a, int b = ) {
int r;
r = a / b;
return (r);
} int main () {
cout << divide () << endl;
cout << divide (, ) << endl; return ;
}
打印结果
YouXianMing
Program ended with exit code:
函数先声明,后使用
#include <iostream>
using namespace std; void odd (int x);
void even (int x); int main() {
int i;
do {
cout << "Please, enter number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=); return ;
} void odd (int x)
{
if ((x%)!=) cout << "It is odd.\n";
else even (x);
} void even (int x)
{
if ((x%)==) cout << "It is even.\n";
else odd (x);
}
递归调用
#include <iostream>
using namespace std; long factorial (long a) {
if (a > )
return (a * factorial (a-));
else
return ;
} int main () {
long number = ;
cout << number << "! = " << factorial (number);
return ;
}
打印结果
! =
Program ended with exit code:
[C++] 用Xcode来写C++程序[4] 函数的更多相关文章
- [C++] 用Xcode来写C++程序[5] 函数的重载与模板
用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespa ...
- [C++] 用Xcode来写C++程序[7] Class
用Xcode来写C++程序[7] Class 不带构造函数的Rectangle类 // // Rectangle.h // Plus // // Created by YouXianMing on 1 ...
- [C++] 用Xcode来写C++程序[6] Name visibility
用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace st ...
- [C++] 用Xcode来写C++程序[3] Constants
用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned ...
- [C++] 用Xcode来写C++程序[2] 操作变量
用Xcode来写C++程序[2] 操作变量 此节讲解包括变量的初始化的几种方式,以及泛型编程的两种变量赋值方式. 最基本的变量赋值以及操作: // operating with variables # ...
- [C++] 用Xcode来写C++程序[1] 新建C++项目工程
用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...
- 使用Xcode IDE写node.js
最近在玩node.js 但是发现很多IDE就是用不顺手 后来发现Xcode可以剖析java script 于是试着使用Xcode来当做node.js的编辑器 首先,在Mac上必须先安装node.js的 ...
- 使用Code::blocks在windows下写网络程序
使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...
- JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数
第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...
随机推荐
- Catalina.createDigester方法详细理解
这个方法主要设置(这个方法很重要,贵在理解,虽然还没学过设计模式..) 1.遇到<server>标签时创建StandardServer实例 设置StandardServer类内部的相关 ...
- Pelican+Github博客搭建详细教程
操作系统:Mac OS / Linux 工具集: 1.Pelican--基于Python的静态网页生成器 2.马克飞象--Evernote出的Markdown文本编辑器 3.GoDaddy--域名供应 ...
- shell:syntax error:unexpected end of file/Starting proxy www-balancer: cannot bind socket--转载
src:http://www.2cto.com/os/201308/238962.html 执行某bash脚本是发生: syntax error: unexpected end of file 主 ...
- jQuery操作<input type="radio">
input type="radio">如下: <input type="radio" name="city" value=&qu ...
- C# 使用/配置Log4Net
1.首先在项目中添加Nuget程序包... 2.然后在NuGet窗体中搜索Log4Net,然后点击安装<安装过程可能会持续几分钟,请耐心等待> 3.在项目中添加一个Config文件,如已有 ...
- Oracle时间换算:日,月,周数,星期,年
http://blog.csdn.net/liangweiwei130/article/details/37930383 Oracle时间换算,留做记号!
- Mac下显示和隐藏隐藏文件的命令
打开终端,输入: 1.defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com. ...
- 十九、curator recipes之PathChildrenCache
简介 curator可以监听路径下子节点的变更操作,如创建节点,删除节点 官方文档:http://curator.apache.org/curator-recipes/path-cache.html ...
- 深入分析 Java 中的中文编码问题【转】
转:https://www.ibm.com/developerworks/cn/java/j-lo-chinesecoding/ 几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是 ...
- HDU 3501 Calculation 2------欧拉函数变形
Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...