• C++日常 function学习


    1. #include<iostream>
    2. #include<cstdio>
    3. using namespace std;
    4. int func(int x)
    5. {
    6. x=x+1;
    7. return x;//如果没有这一行,则输出的z=7,如果有,则z=8
    8. }
    9. int main()
    10. {
    11. int x;
    12. x=7;
    13. cout<<"x="<<x<<endl;
    14. int y=7;
    15. int z=func(y);
    16. cout<<"z="<<z<<endl;
    17. return 0;
    18. }

    上面代码主要用于测试:

    1.内外变量

    2.外函数是否返回值,返回值有什么区别

    3.主函数必须放在程序的最后,因为主函数引用了上面的自定义函数

    1. #include<iostream>
    2. #include<cstdio>
    3. using namespace std;
    4. int f(int& a) { a = a+1; return a; }
    5. //如果是(int& a)则代表的地址,而不是值
    6. //如果是int f(int a) { a = a+1; return a; }
    7. //则传递的数值,下面xx还是0,yy还是7.
    8. int main()
    9. {
    10. int xx = 0;
    11. cout << f(xx) << endl; // writes 1
    12. // f() changed the value of xx
    13. cout << xx << endl; // writes 1
    14. int yy = 7;
    15. cout << f(yy) << endl; // writes 8
    16. // f() changes the value of yy
    17. cout << yy << endl; // writes 8
    18. }

    注意上面代码的测试,主要特点在于int f(int& a) { a = a+1; return a; }  

    &a传递的是地址,下面引用函数的时候,改变的不仅仅是一个数值,而是地址.

    1. #include<iostream>
    2. #include<cstdio>
    3. using namespace std;
    4. int incr1(int a) { return a+1; }
    5. void incr2(int& a) { ++a; }
    6. int main(){
    7. int x = 7;
    8. int y = incr1(x); // pretty obvious
    9. incr2(x); // pretty obscure
    10. cout<<y<<endl;
    11. cout<<x<<endl;
    12. }

    注意两个函数,incr1函数返回了数值,可以用来进行数值传递

    而incr2函数并没有返回数值,但是他传递了地址,同样也完成了数值的++;

    1. #include<iostream>
    2. #include<cstdio>
    3. using namespace std;
    4. int main(){
    5. int i = 7;
    6. int& r = i;//注意地址传递
    7. r = 9; // i becomes 9
    8. cout<<i<<endl;
    9. const int& cr = i;
    10. // cr = 7; // error: cr refers to const
    11. i = 8;
    12. cout << cr << endl; // write out the value of i (that’s 8)
    13. }

    注意 int& r=i;类程序的运用,如果修改i的数值,则r也将跟着改变,如果修改r的数值,i的数值也将跟着改变.

    但在第二个程序当中,也就是const int& cr = i;不可以修改cr的数值,因为cr所在的内存是一个定量,不可以进行数值修改,但是可以修改i的数值,以此来改变cr定量的数值.

    1. namespace Jack { // in Jack’s header file
    2. class Glob{ /**/ };
    3. class Widget{ /**/ };
    4. }
    5. namespace Jill { // iillill’s header file
    6. class Glob{ /**/ };
    7. class Widget{ /**/ };
    8. }
    9. #include "jack.h"; // this is in your code
    10. #include "jill.h"; // so is this
    11. void my_func(Jack::Widget p) // OK, Jack’s Widget class will not
    12. { // clash with a different Widget
    13. //
    14. }

    注意void my_func(Jack::Widget p)  Widget.p在上面有两个函数都有这个函数,所以前面必须添加Jack::这样的引用,可以使得程序选定某一个函数,而不是在两个Jack和Jill函数之间进行徘徊,这样会产生错误.

    例如:

     cout 在名字空间 std中,你可以写成下面的形式:           

     std::cout << "Please enter stuff… \n“;

  • 相关阅读:
    第一百三十九回 介绍三个BLE包
    读书笔记:从缺陷中学习C++
    Jenkins Pipeline 方法(函数)定义及调用
    【Vue】Vue项目需求--实现搜索框输入防抖处理
    力扣-383.赎金信
    Linux Cgroup 系列:CentOS 7 Systemd Cgroup 层级
    数据结构:树和二叉树的概念及性质
    CS创世 SD NAND与SPI NAND的对比
    string类接口介绍及应用
    C51单片机使用1-工程创建和Led闪烁灯
  • 原文地址:https://blog.csdn.net/Lukegood/article/details/127811962