本帖最后由 bin554385863 于 2019-10-7 11:51 编辑
七
************************************************
----自定义函数----
函数声明(有返回值):
返回值类型 函数名(形参列表);
函数定义:
返回值类型 函数名(形参列表)
{
函数体;
return 返回值;
}
-----------------------------------------------
函数声明(无返回值):
[i]void 函数名(形参列表);
函数定义:
void 函数名(形参列表)
{
函数体;
//没有return语句;
}#include <iostream>
int max(int a, int b)
{
//有返回值的函数
return a > b ? a : b;
}
void _swap(int &a, int &b)
{
//没返回值的函数,
int t = a;
a = b;
b = t;
}
int main(int argc, char const *argv[])
{
int a = 10, b = 100;
std::cout << "原数据" << '\n'
<< "a = " << a << '\n'
<< "b = " << b << std::endl;
std::cout << "最大值为" << '\n'
<< max(a, b) << std::endl;
_swap(a, b);
std::cout << "数值交换" << '\n'
<< "a = " << a << '\n'
<< "b = " << b;
return 0;
}
[i](int &a, int &b)表示参数是一个变量的引用,效果等同于指针;
不同的是指针是一个对象,而引用不是;指针可以改变指向,而引用不行;
引用是和变量绑定的.
------------------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.25.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-0v4irou2.n3l --stdout=Microsoft-MIEngine-Out-lu5e3moa.qjj --stderr=Microsoft-MIEngine-Error-11yrrpp2.fa2 --pid=Microsoft-MIEngine-Pid-aydf0r4r.2pa "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
原数据
a = 10
b = 100
最大值为
100
数值交换
a = 100
b = 10
E:\Users\86184\Documents\Code>
-------------------------------------------------------------------------------
[i]自定义函数在文件开头定义时可以不声明;
后面才定义的函数一定要在文件开头声明.
=======================================================================
---函数指针
type function(args list)
{
function body;
}
type (*ptr)(args list)
ptr = function;//指向函数
也可以
type (*ptr)(args list) = function;//直接指向
//参数类型,和个数必须和指向的函数一模一样;#include <iostream>
void _swap(int &a, int &b)
{
int t = a;
a = b;
b = t;
}
int main(int argc, char const *argv[])
{
int a = 41, b = 96;
void (*ptr)(int &a, int &b) = _swap;//定义一个函数指针
//ptr = _swap;//指向一个函数
std::cout << "原数据" << '\n'
<< "a = " << a << '\n'
<< "b = " << b<<'\n';
ptr(a, b);//使用指针调用函数
std::cout << "ptr(a, b)" << '\n'
<< "a = " << a << '\n'
<< "b = " << b;
return 0;
}
-----------------------------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.25.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-025q3y2h.vc5 --stdout=Microsoft-MIEngine-Out-15kpp1nx.a2o --stderr=Microsoft-MIEngine-Error-mnxxakk3.kbs --pid=Microsoft-MIEngine-Pid-b2csjtgi.mlv "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
原数据
a = 41
b = 96
ptr(a, b)
a = 96
b = 41
E:\Users\86184\Documents\Code>
---------------------------------------------------------------------------------------------------------------
----函数指针的作用不止可以用来调用函数,还可以作为函数的参数/*函数指针*/
#include <iostream>
int max(int &a, int &b)
{
return a > b ? a : b;
}
void sum(int i, int j, int x, int y, int (*ptr)(int &a, int &b)) //函数指针作为参数
{
ptr = max; //指向函数
printf("%d + %d = %d", ptr(i, j), ptr(x, y), ptr(i, j) + ptr(x, y));
}
int main(int argc, char const *argv[])
{
int a = 41, b = 96;
int i = 52, j = 32;
int (*ptr)(int &a, int &b); //定义函数指针
sum(a, b, i, j, ptr);//传入函数
return 0;
}
-----------------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.25.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-aaj1q15x.ipf --stdout=Microsoft-MIEngine-Out-z1zinsyb.gc2 --stderr=Microsoft-MIEngine-Error-oiwb5cle.qm5 --pid=Microsoft-MIEngine-Pid-ptuabdxa.ueg "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
96 + 52 = 148
E:\Users\86184\Documents\Code> |