C++学习路程 22/3/5 AM 11:07

#include <iostream>
using namespace std;
//函数  function
    /*  返回值类型  函数名(所需输入参数 , 所需输入参数 , 所需输入参数)
    {
        函数体语句;

        return 返回值;
    } */
    int  add(int num1, int num2)
    {
        int sum = num1 + num2;
        return sum;
    }

    //函数调用
    int main() {
    int a = 1;
    int b = 2;
    int c = add(a, b); //函数调用
    cout<<c<<endl;


    //值传递
 void swap(int num1, int num2)//不需要返回值,void声明
{   
    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 = 1;
    int b = 2;
    cout << "a =" << a << endl;
    cout << "b =" << b << endl;

    swap(a, b);//调用函数,实参不改变,函数自己会生成新的形参来进行工作
    cout << "a =" << a << endl;
    cout << "b =" << b << endl;



    system("pause");
    return 0;

}

    //函数形式
//   无参无返
void test01()
{
    cout << "a" << endl;
}
// 有参无返
void test02(int a)
{
    cout << a << endl;
}
// 无参有返
int test03()
{
    cout << "a" << endl;
    return 10;
}
// 有参有返
int test04(int num)
{
    cout << num << endl;
    return num;
}
int main()
{
    test01();

    test02(10);
    
    int c =test03();
    cout << c<< endl;

    int b=test04(10);
    cout << b << endl;

    system("pause");
}



//函数声明
// 主函数在前;函数在后面;需要前面声明一下
int max(int a, int b);
int main()
{
    cout << max(1, 2) << endl;
}
int max(int num1, int num2)
{
    int ans = num1 < num2 ? num2 : num1;
    return ans;
}


     system("pause");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值