[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 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...
随机推荐
- Java直接用javac来编译带package的类
在没有package语句的java类, 我们可以直接使用: javac Test.java 就可以了, 如果Test.java中包含package语句,如:package abc; 编译后,是要求Te ...
- CentOS 7 安装 RabbitMQ 3.7
目录 CentOS 7 安装 RabbitMQ 3.7 安装Erlang 安装依赖 创建yum源 参考 添加内容 安装 进入erlang命令行表示成功 安装 socat RabbitMQ 安装 sys ...
- redis实战笔记(5)-第5章 使用 Redis构建支持程序
本章主要内容 1.使用Redis记录日 志 2.使用Redis实现计数器并进行数据统计 3.查询IP地址所属的城市与国家 4.服务的发现与配置 这一章将介绍如何使用Redis来帮助和支持系统的其他 ...
- ruby 中的 module
Module是Class的父类: >> Class.superclass => Module module 没有实例变量 module 没有new不能生成实例对象 module内可以 ...
- win7 iis7 asp.net 编译器错误消息: CS0016:
编译器错误消息: CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root ...
- zTree中 checkbox 点击向文本框中赋值
例子如下:第一个 文本框: <div class="content_wrap" style="height: 0;position: relative; " ...
- dynamic结合匿名类型 匿名对象传参
首先说明下,我一般很少用dynamic关键字(类)的,因为毕竟是由反射实现的,所以对于性能方面还是吃亏不少(注:由于心里没底,查了一些资料得知,dynamic实质上好像不是由反射实现的,其性能也比直接 ...
- 撩课-Web大前端每天5道面试题-Day18
1.如何判断一个变量是对象还是数组? 判断数组和对象分别都有好几种方法,其中用prototype.toString.call()兼容性最好. function isObjArr(variable){ ...
- 微软官方公布的Windows 8.1 Update常用快捷键
以前用 Windows Server 2008R2,初装Win8.1,感觉最明显的是开关机速度真心快~下面摘录了常用的几个快捷键: Windows 键+D:显示或隐藏桌面 Windows键+X:访问Q ...
- spring 中 InitializingBean 接口使用理解
前言:这两天在看 spring 与 quart 的集成,所以了解到 spring 是如何初始化 org.springframework.scheduling.quartz.SchedulerFacto ...