当前位置: 首页 > news >正文

函数(个人学习笔记黑马学习)

1、函数定义

#include <iostream>
using namespace std;int add(int num1, int num2) {int sum = num1 + num2;return sum;
}int main() {system("pause");return 0;
}

2、函数的调用

#include <iostream>
using namespace std;int add(int num1, int num2) {int sum = num1 + num2;return sum;
}int main() {int a = 10;int b = 20;int sum = add(a, b);cout << sum << endl;system("pause");return 0;
}

 


3、值传递

#include <iostream>
using namespace std;void swap(int num1, int num2) {cout << "交换前" << endl;cout << "num1=" << num1 << endl;cout << "num2=" << num2 << endl;int temp = num1;num1 = num2;num2 = temp;cout << "交换后" << endl;cout << "num1=" << num1 << endl;cout << "num2=" << num2 << endl;
}int main() {int a = 10;int b = 20;swap(a, b);cout << "a=" <<a<< endl;cout << "b=" << b << endl;system("pause");return 0;
}

 


4、函数的常见样式

#include <iostream>
using namespace std;//1、无参无返
void test01() {cout << "this is test01" << endl;
}//2、有参无返
void test02(int a) {cout << "this is test02 a = " << a << endl;
}//3、无参有返
int test03() {cout << "this is test03" << endl;return 1000;
}//4、有参有返
int test04(int a) {cout << "this is test04 a = " << a << endl;return a;
}int main() {//无参无返的函数调用test01();//有参无返的函数调用test02(100);//无参有返的函数调用int num1=test03();cout << "num1 = " << num1 << endl;//有参有返的函数调用int num2=test04(10000);cout << "num2 = " << num2 << endl;system("pause");return 0;
}


5、函数声明 

#include <iostream>
using namespace std;int max(int a, int b);int main() {int a = 10;int b = 20;cout << max(a, b) << endl;system("pause");return 0;
}int max(int a, int b) {return a > b ? a : b;
}


6、函数的分文件编写 

函数分文件编写一般有4个步骤
1.创建后级名为.h的头文件
2.创建后缀名为.cpp的源文件
3.在头文件中写函数的声明
4.源文件中写函数的定义

 

 swap.h

#include <iostream>
using namespace std;void swap(int a, int b);

swap.cpp

#include "swap.h"void swap(int a, int b) {int temp = a;a = b;b = temp;cout << "a = " << a << endl;cout << "b = " << b << endl;}

main.cpp

#include <iostream>
using namespace std;
#include "swap.h"int main() {int a = 10;int b = 20;swap(a, b);system("pause");return 0;
}

http://www.lryc.cn/news/145980.html

相关文章:

  • 《Flink学习笔记》——第五章 DataStream API
  • Vue3.0 新特性以及使用变更总结
  • ToBeWritten之VSOC安全运营
  • 2023爱分析·一站式通信解决方案市场厂商评估报告:牛信云
  • 微信小程序消防知识每天学平台设计与实现
  • Oracle跨库访问DBLINK
  • 【vue3.0 组合式API与选项式API是什么,有什么区别】
  • React配置代理的5种方法
  • 皮卡丘靶场搭建遇到的问题大全
  • 【C++】C++11的新特性(上)
  • ubuntu学习(四)----文件写入操作编程
  • 如何解决MySQL中的套接字错误
  • socket
  • Python数据分析实战-判断一组序列(列表)的变化趋势(附源码和实现效果)
  • Spring与MyBatis集成 AOP整合PageHelper插件
  • [Android 四大组件] --- BroadcastReceiver
  • <C++> STL_容器适配器
  • 【25考研】- 整体规划及高数一起步
  • 【Unity】常见的角色移动旋转
  • 今天的小结
  • 了解 Socks 协议:它的过去、现在与未来
  • 小谈静态类和单例模式
  • ​LeetCode解法汇总823. 带因子的二叉树
  • TypeScript的变量声明的各种方式
  • c++ lambda
  • 泊松回归和地理加权泊松回归
  • 【数学建模竞赛】各类题型及解题方案
  • 【12期】谈一谈redis两种持久化机制的区别?
  • Lambda 编程(Kotlin)一
  • 网络字节序——TCP接口及其实现简单TCP服务器