1.10 c++_函数

c++ 定义函数的通用格式:
void functionName (parameterList){
statement(s);
return;
}

调用函数:

#include <iostream>

void test1(int);//声明函数的时候,参数可以只写类型
double test2(double a);
void test3(string);
void test4(int*);
void test5(int[]);
void test6(const char,int);

int main(){
    using namesapce std;
    test1(5);
    double testd;
    cin >> testd;
    cout << "test2模运算的值:" << test2(testd) << endl;
    test3();
    int is[5] = {1,2,3,4,5};
    test5(is);
    char str[20]="this's test function";
    test6(str,20);
    return 0;
}

void test1(int i){
    cout << "传递进来的int型值是:" << i << endl;
}

double test2(double d){
    return d%2;//模运算
}

//c++的函数可以给参数赋默认值,这一点和java有所区别,java是不能这样做的
void test3(string str = "empty"){
    cout << "传递进来的字符串是:" << str << endl;
}

void test4(int* i){
    cout << "用函数传递指针:" << *i << endl;
}

void test5(int[] is){
    if(sizeof(is) == 0)return;
    for(int i = 0; i < sizeof(is); i++){    
        is[i] = i + 10;//修改数组的元素
        cout << "修改后的数组:" << is[i] << endl;
        is[i]* = i + 15;//修改数组的元素
        cout << "修改后的数组:" << is[i] << endl;
    }
}

//c-风格字符串
void test6(const char* str,int i){
    while(*str){//从指针中获取字符
        cout <<"字符:" << *str << endl;
        str++;
    }
    while(i --> 0){//这意味着将从i的值一直循环到0才结束
        cout << "字符:" << str[i] << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值