[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 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...
随机推荐
- Self-Attention与Transformer
直观理解与模型整体结构 先来看一个翻译的例子“I arrived at the bank after crossing the river” 这里面的bank指的是银行还是河岸呢,这就需要我们联系上下 ...
- Docker数据管理(数据卷&数据卷容器)
生产环境中使用Docker的过程中,往往需要对数据进行持久化,或者需要在多个容器之间进行数据共享,这必然涉及容器的数据管理操作. 容器中管理数据主要有两种方式: 数据卷(Data Volumes):容 ...
- 使用vue2+Axios+Router 之后的总结以及遇到的一些坑
构建 vue有自己的脚手架构建工具vue-cli,使用起来非常方便,使用webpack来集成各种开发便捷工具,比如: 代码热更新,修改代码之后网页无刷新改变,对前端开发来说非常的方便 PostCss, ...
- Spring IOC(DI)
软件152 余建强 1 什么是IOC IOC—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不 ...
- css3的overflow-anchor
overflow-anchor属性使我们能够选择退出滚动锚定,这是一个浏览器特性,旨在允许内容在用户当前的DOM位置上加载,而不需要在内容完全加载后更改用户的位置. 为何要有这个属性? 滚动锚定是一种 ...
- ArchLinux - 安装指南
Step 1 将镜像写入u盘 u盘从来不是唯一的选择,但多数人可能喜欢这么做. 我是在OS X上进行操作,如果你用的是windows,也许可以使用Image Writer for Windows或者U ...
- 【转】C#中continue、break和return用法
continue和break的用法一样,直接写上这个单词,后面加一个分号就行 比如: continue; break; 我们先来谈continue 看代码 for (int i=0; i<10; ...
- 四:Jquery-animate
动画效果: 1.显示/隐藏动画效果 动态的改变当前元素的宽,高和不透明度 show([duration],[fn]); //显示当前元素 hide([duration],[fn]); //隐藏当前元素 ...
- SSM(Spring+SpringMVC+Mybstis)搭建,写给新手
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 作用: SSM框架是spring MVC ,spring和mybatis框架的整合,是标准的MVC模式,将整个系统划分为 ...
- Linux线程同步——条件变量
互斥锁是用来给资源上锁的,而条件变量是用来等待而不是用来上锁的. 条件变量用来自动阻塞一个线程,直到某特殊情况发生为止. 通常条件变量和互斥锁同时使用. 和条件变量使用有关的几个重要函数: int p ...