用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. JavaScript 常用的小功能集合

    1. 得到当前用户使用的浏览器的内核版本 function getExplorer(){ var browser = ""; var explorer = window.navig ...

  2. Asterist搭建步骤

    环境: # cat /proc/version Linux version 2.6.18-308.el5 (mockbuild@x86-010.build.bos.redhat.com) (gcc v ...

  3. C#中的序列化和反序列化是什么、有什么作用、使用方法详解

    什么是序列化与反序列化??? 序列化和反序列化,我们可能经常会听到,其实通俗一点的解释,序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用. ...

  4. SQL Server操作结果集-并集 差集 交集 结果集排序

    操作结果集 为了配合测试,特地建了两个表,并且添加了一些测试数据,其中重复记录为东吴的人物. 表:Person_1魏国人物 表:Person_2蜀国人物 A.Union形成并集 Union可以对两个或 ...

  5. 什么是SOA

    面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.接口是采用中立的方式进行定义的,它应该独立于实现服务的硬件平台.操作系统和编 ...

  6. solr + tomcat + mysql整合

    上一次分享了solr+tomcat的整合 学习就是要一步一步的进行才有趣 所以这次给大家分享solr+tomcat+mysql 一.准备工作 1.一张带数据的数据库表(我用的是这张叫merchant的 ...

  7. mac 下mongodb connect failed 连接错误

    我是用brew install mongod 安装的 MongoDB shell version v3.4.2connecting to: mongodb://127.0.0.1:270172017- ...

  8. 【学习笔记】--- 老男孩学Python,day12 函数名的应用,闭包,迭代器

    1, 函数名的应用,第一类对象 函数名可以像变量一样进行使用 1.赋值 def func(): print("你吃了么?") # print(func) # a = func # ...

  9. python学习之老男孩python全栈第九期_第二周学习总结

    迭代器 双下方法:很少直接调用的方法,一般情况下,是通过其他语法触发的 1. 可迭代的 --> 可迭代协议:含有__iter__的方法( '__iter__' in dir(数据) ) 可迭代的 ...

  10. Algorithm——两个排序数组的中位数

    ps:城际的网速还是不错的-