4.1 函数
语法:返回值类型 函数名 参数列表
函数体语句
return表达式
示例:
加法函数:实现两个整形相加,并且将相加的结果返回
int add (int num1, int num2){
int sum = num1 +num2;
return sum;
}
4.1.1函数的调用
语法:函数名 (参数)
示例:
调用上面的加法函数
#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;
//调用add函数
int sum = add(a, b);
cout << "sum =:" << endl;
system("pause");
return 0;
}
4.1.2值传递
定义:就是函数调用时实参将数值传给形参,值传递时,如果形参发生,实参并不会受到影响
示例:
#include <iostream>
using namespace std;
void swap(int num1, int num2) {
cout << "交换前" << endl;
cout << "num1=" << num1 << endl;
cout<< "num2=" << num2<< endl;
int tmp = num1;
num1 = num2;
num2 = tmp;
cout << "交换后"<<endl;
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
//return; 当函数声明的时候,不需要返回值,可以不写return
}
int main()
{
int a = 10;
int b = 20;
cout << "a =" << a << endl;
cout << "b =" << b << endl;
swap(a, b);
cout << "a =" << a << endl;
cout << "b =" << b << endl;
system("pause");
return 0;
}
4.1.3函数的常见样式
常见的函数样式有四种:
1.无参无返; 2.有参无返; 3.无参有返; 4.有参有返
示例:
int test03(){
cout << "this is test03 " << endl;
return 100;
}
//4.有参有返;
int test04(int a) {
cout << "this is test04 a= " << a << endl;
return a;
}
int main()
{
//无参无返的调用
test01();
//有参无返的调用
test02(100);
//无参有返的调用
test03();
//有参有返的调用
int num = test04(100);
cout << "num = " << num << endl;
system("pause");
return 0;
}
4.1.4函数的声明
函数的声明可以多次,但是函数的定义只有一次
示例:
#include <iostream>
using namespace std;
//声明
int max(int a, int b);
int max(int a, int b);
//定义
int max(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int a = 10;
int b = 20;
cout << max(a, b) << endl;
system("pause");
return 0;
}
4.1.5函数的分文件编写
步骤;
1.创建后缀名为.h的头文件
2.创建后缀名为.cpp的源文件
3.在头文件中写函数的声明
4.在源文件写函数的定义
4.2指针
作用:可以通过指针间接访问内存;内存编号是从0开始记录的,一般用十六进制数字表示;可以利用指针变量保存地址
4.2.1指针变量的定义与使用
语法:数据类型 * 变量名;
示例:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
//定义指针
int* p;
//指针记录变量a的地址
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "指针p为:" << p << endl;
//使用指针
//可以通过解引用的方式来找到指针指向的内存
//指针前加 * 代表解引用
*p = 1000;
cout << "a = " << a << endl;
cout << "*p =" << *p << endl;
system("pause");
return 0;
}
4.2.2指针所占的内存空间
在32位操作系统下:占用4个字节空间;
在64位操作系统下:占用8个字节空间。
4.2.3空指针和野指针
空指针:指针变量指向内存编号为0的空间
用途:初始化指针变量
注意:空指针指向的内存是不能访问的
示例:
#include <iostream>
using namespace std;
int main()
{
int * p = NULL;
//访问空指针报错
//内存编号0~255为系统占用内存,不允许用户访问
cout << "*p" << endl;
system("pause");
return 0;
}
野指针:指针变量指向非法的内存空间
示例:
#include <iostream>
using namespace std;
int main()
{
//指针变量p指向内存地址编号为0x1100的空间
int * p = (int*)0x1100;
//访问野指针报错
cout << "*p" << endl;
system("pause");
return 0;
}
4.2.4const修饰指针
const修饰指针有三种情况:
1.const修饰指针 ---常量指针;
2.const修饰变量 ----指针常量;
3.const即修饰指针,又修饰常量;
示例:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
//const修饰的是指针,指针指向可以修改,指针指向的值不可以改变
const int* p1 = &a;
p1 = &b;//正确
// * p1 = 100;错误
//const修饰的是常量,指针指向不可以更改,指针指向的值可以更改
int* const p2 = &a;
//p2 = &b;错误
*p2 = 100;//正确
//指针即修饰常量又修饰指针,指针的指向和指向的值都不能改
const int* const p3 = &a;
// *p3 = 100;错误
// p3 = &b;错误
system("pause");
return 0;
}
4.2.5指针和数组
作用:利用指针访问数组中的元素
示例
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9, };
int* p = arr;
cout << "第一个元素:" << arr[0] << endl;
cout << "指针访问第一个元素:" << *p << endl;
for (int i = 0; i < 9; i++) {
//利用指针遍历数组
cout << *p << endl;
p++;
}
system("pause");
return 0;
4.2.6指针和函数
作用:利用指针作为函数参数,可以修改实参的值
示例
#include <iostream>
using namespace std;
//值传递
void swap1(int a, int b) {
int temp = a;
a = b;
b = temp;
}
//地址传递
void swap(int* p1, int * p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main()
{
int a = 10;
int b = 20;
/*值传递
swap1(a, b);*/
//地址传递
swap(&a, &b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
system("pause");
return 0;
}
4.2.7指针、数组、函数
写个demo:封装一个函数,利用冒泡排序,实现对整型数组的升序排序
示例:
#include <iostream>
using namespace std;
void swap(int* arr, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int* arr, int len) {
for (int i = 0; i < len; i++) {
cout << arr[i] << endl;
}
}
int main()
{
int arr[10] = { 1,8,3,4,7,9,10,6,5,2 };
int len = sizeof(arr) / sizeof(arr[0]);
swap(arr, len);
printArray(arr, len);
system("pause");
return 0;
}