菜鸟教程2

八、函数
#include <iostream>
using namespace std;
int max(int , int );
int main()
{
	int a = 100;
	int b = 20;
	int ret;
	ret = max(a, b);
	cout << "最大值是:" << ret << endl;
	return 0;
}
int max(int num1, int num2)
{
	int result;
	if (num1 > num2)
		result = num1;
	else
		result = num2;
	return result;
}

函数声明   int max(int , int );参数的名称并不重要,只有参数的类型是必需的


函数参数   

如果函数要使用参数,则必须声明接受参数值的变量。这些变量称为函数的形式参数
形式参数就像函数内的其他局部变量,在进入函数时被创建,退出函数时被销毁。

默认情况下,C++ 使用传值调用来传递参数。一般来说,这意味着函数内的代码不能改变用于调用函数的参数。


值传递:

void Func1(int x) 

x = x + 10; 

... 
int n = 0; 
Func1(n); 
cout << "n = " << n << endl; // n = 0 


指针传递

void Func2(int *x) 

(* x) = (* x) + 10; 

... 
int n = 0; 
Func2(&n); 
cout << "n = " << n << endl; // n = 10 


引用传递

void Func3(int &x) 

x = x + 10; 

... 
int n = 0; 
Func3(n); 
cout << "n = " << n << endl; // n = 10 

1、值传递

把参数的实际值复制给函数的形式参数。在这种情况下,修改函数内的形式参数不会影响实际参数

#include <iostream>
using namespace std;
void swap(int x, int y);
int main()
{
	int a = 100;
	int b = 100;
	cout << "交换前" << a;
	cout << b << endl;
	swap(a, b);
	cout << "交换后" << a;
	cout << b << endl;
	return 0;

}
void swap(int x, int y)
{
	int temp;
	temp = x;
	x=y;
	y = temp;
	return;
	
}
交换前:100 200   交换后 :100 200

2、指针传递

把参数的地址复制给形式参数。在函数内,该地址用于访问调用中要用到的实际参数。这意味着,修改形式参数会影响实际参数

#include <iostream>
using namespace std;
void swap(int *x, int *y);
int main()
{
	int a = 100;
	int b = 200;
	cout << "交换前" << a;
	cout << b << endl;
	swap(&a, &b);
	cout << "交换后" << a;
	cout << b << endl;
	return 0;

}
void swap(int *x, int *y)
{
	int temp;
	temp = *x;
	*x=*y;
	*y = temp;
	return;
	
}

交换前 100 200  交换后:200  100


3、引用传递

#include <iostream>
using namespace std;
void swap(int &x, int &y);
int main()
{
	int a = 100;
	int b = 200;
	cout << "交换前" << a;
	cout << b << endl;
	swap(a,b);
	cout << "交换后" << a;
	cout << b << endl;
	return 0;

}
void swap(int &x, int &y)
{
	int temp;
	temp = x;
	x=y;
	y = temp;
	return;
	
}
声明函数时将指针的*替换为&。

交换前 100 200  交换后 200 100

参数默认值

当调用函数时,如果实际参数的值留空,则使用这个默认值。

Lambda函数

Lambda 表达式把函数看作对象。Lambda 表达式可以像对象一样使用,比如可以将它们赋给变量和作为参数传递,还可以像函数一样对其求值。
Lambda 表达式本质上与函数声明非常类似

[](int x, int y){ return x < y ; }

九、C++数字

数字运算

为了利用这些函数,您需要引用数学头文件 <cmath>。

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	short s = 10;
	int i = -1000;
	long l = 1000;
	float f = 230.47;
	double d = 200.374;
	cout << "sin(d)" << sin(d) << endl;
	cout << "abs(i)" << abs(i) << endl;
	cout << "floor(d) :" << floor(d) << endl;
	cout << "sqrt(f) :" << sqrt(f) << endl;
	cout << "pow( d, 2) :" << pow(d, 2) << endl;
	return 0;
}
double pow(double, double);  假设第一个参数为 x,第二个参数为 y,则该函数返回 x 的 y 次方。
double sqrt(double);  该函数返回参数的平方根。

double hypot(double, double);该函数返回两个参数的平方总和的平方根,也就是说,参数为一个直角三角形的两个直角边,函数会返回斜边的长度。

double floor(double);该函数返回一个小于或等于传入参数的最大整数


十、数组

1、声明数组

double balance[10];
需要指定元素的类型和元素的数量

2、初始化数组

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
0作为索引的第一个数字,数组的索引是数组的实际大小减去 1。

3、二维数组

#include <iostream>
using namespace std;
int main()
{
	int a[5][2] = { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 5, 6 }, { 5, 8 } };
	for (int i = 0; i <= 4; i++)
		for (int j = 0; j <= 1; j++)
		{
		cout << "a[" << i << "][" << j << "]:";
		cout << a[i][j] << endl;
		}
	return 0;

}
4、 指向数组的指针

#include <iostream>
using namespace std;
int main()
{
	double bal[5] = { 1000.0, 2.0, 3.4, 17.0, 50.0 };
	double *p;
	p = bal;

	for (int i = 0; i < 5; i++)
	{
		cout << "*(p+" << i << ");";
		cout << *(p + i) << endl;
	}
	return 0;
}
一旦我们有了 p 中的地址,*p 将给出存储在 p 中相应地址的值。

五、传递数组给函数(数组作为参数)

void myFunction (int param[])

#include <iostream>
using namespace std;
double get(int arr[], int size);
int main()
{
	int bal[5] = { 1000, 2, 3, 17, 50 };
	double avg;
	avg = get(bal, 5);
	cout << "平均值是:" << avg << endl;
	return 0;
}
double get(int arr[], int size)
{
	int i, sum = 0;
	double avg;
	for (i = 0; i < size; i++)
	{
		sum += arr[i];
	}
	avg = double(sum) / size;
	return avg;
}


十一、指针

指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址。定义一个指针变量、把变量地址赋值给指针、访问指针变量中可用地址的值。

#include <iostream>
using namespace std;
int main()
{
	int var = 20;
	int *ip;
	ip = &var;
	cout << "Value of var variable: ";
	cout << var << endl;
	// 输出在指针变量中存储的地址
	cout << "Address stored in ip variable: ";
	cout << ip << endl;
	// 访问指针中地址的值
	cout << "Value of *ip variable: ";
	cout << *ip << endl;
	return 0;
}
ip=&var;单纯的ip数值是var地址的数值。

*ip=var;

&取地址,* 取值。


1、NULL值(空指针)

 int  *ptr = NULL;
在变量声明的时候,如果没有确切的地址可以赋值,为指针变量赋一个 NULL 值是一个良好的编程习惯。

2、指向指针的指针

int **var;
3、传递指针给函数(指针作为函数的参数)

先定义函数的形参为指针类型,再在调用函数的地方把实参传入即可,实参要传递指针的地址,这样就把指针传递到函数中了。

声明:int func(char *c, int *i)
调用:char *p;
            int i;
           func(p, &i);






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值