用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] 函数的更多相关文章

  1. [C++] 用Xcode来写C++程序[5] 函数的重载与模板

    用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespa ...

  2. [C++] 用Xcode来写C++程序[7] Class

    用Xcode来写C++程序[7] Class 不带构造函数的Rectangle类 // // Rectangle.h // Plus // // Created by YouXianMing on 1 ...

  3. [C++] 用Xcode来写C++程序[6] Name visibility

    用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace st ...

  4. [C++] 用Xcode来写C++程序[3] Constants

    用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned ...

  5. [C++] 用Xcode来写C++程序[2] 操作变量

    用Xcode来写C++程序[2] 操作变量 此节讲解包括变量的初始化的几种方式,以及泛型编程的两种变量赋值方式. 最基本的变量赋值以及操作: // operating with variables # ...

  6. [C++] 用Xcode来写C++程序[1] 新建C++项目工程

    用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...

  7. 使用Xcode IDE写node.js

    最近在玩node.js 但是发现很多IDE就是用不顺手 后来发现Xcode可以剖析java script 于是试着使用Xcode来当做node.js的编辑器 首先,在Mac上必须先安装node.js的 ...

  8. 使用Code::blocks在windows下写网络程序

    使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...

  9. JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数

    第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...

随机推荐

  1. android学习-LocationManager(一)-定位方式原理解析

    参考资源:android 4种定位原理及实现——1 android使用不同的方法为应用提供位置信息. 定位的方式有三种:GPS地位(A-GPSAssistedGPS:辅助全球卫星定位系统,或者是同步G ...

  2. WPF Style和Template

    WPF中的Style类似于Web应用程序中的CSS,它是控件的一个属性,属于资源的一种. ControlTemplate和DataTemplate区别: ControlTemplate用于改变控件原来 ...

  3. zabbix邮件内容乱码与邮件内容为附件解决办法

    在zabbix的实际使用过程中,在收到邮件预警的时候,我们会发现邮件内容是乱码的,在手机端收到的是附件,而且附件下载后的文件类型是打不开的.这样我们不知道我们是哪个服务器的哪项服务出了问题,接下来我们 ...

  4. Oracle 12c 建表空间语句

    create tablespace TBS_DYS datafile 'D:/Oracle_12c/app/dingyingsi/oradata/dingyingsi/TBS_DYS.ba' size ...

  5. C#学习之文件操作

    1 DirectoryInfo 类介绍   DirectoryInfo 类在 .net 开发中主要用于创建.移动和枚举目录和子目录的实例方法,此类不能被继承. 从事 .net 软件开发的同事对 Dir ...

  6. CocoaPods管理第三方

    之前听伟哥说用CocoaPods做第三方库的管理很方便,今天看了下自己做了下感觉确实不错.下面开始,Let's go!! 1.安装CocoaPods之前,先确保本地有Ruby环境,因为CocoaPod ...

  7. Spring MVC之源码速读之RequestMappingHandlerAdapter

    spring-webmvc-4.3.19.RELEASE 下面来看DispatcherServlet中的执行: /** * Exposes the DispatcherServlet-specific ...

  8. web前端--实现前后端分离的心得

    1.实现前后端分离的心得 2.前后端分离实践 3.谈谈前后端的分工协作 4.从MVC到前后端分离(REST-个人也认为是目前比较流行和比较好的方式) 4.1.REST风格框架实战:从MVC到前后端分离 ...

  9. [转]SQL Server如何启用xp_cmdshell组件

    本文转自:https://www.cnblogs.com/atree/p/SQL_SERVER_xp_cmdshell.html 错误描述]: SQL Server阻止了对组件‘xp_cmdshell ...

  10. 简易HashMap实现

    为了更好的理解HashMap线程不安全的根源,这里提供了HashMap的简易实现: package map.test; import org.apache.commons.lang3.StringUt ...