1.10 c++_函数

来源:互联网 发布:生死狙击矩阵图片 编辑:程序博客网 时间:2024/06/02 07:05

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;    }}
原创粉丝点击