函数的总结1

1.函数的定义

class 函数名(){

表达式

};`

####2.函数的四种形式

#include <iostream>
#include <string>

using namespace std;


// 无参无返回值
void setHi() {
	cout << "你好···" << endl;
}

// 无参有返回值
string saySorry() {
	return "对不起了。。。";
}

// 有参无返回值
void printIN(string name) {
	cout << "我的名字是:" << name << endl;
}

// 有参有返回值
string getMsg(string msg) {
	return "我的消息是:" + msg;
}


int main() {

	setHi();
	cout << saySorry() << endl;
	printIN("小明");
	cout << getMsg("你好") << endl;

	return 0;
}
3.分离式编程
// stu3.h

#pragma once
//#include <iostream>
#include <string>

using namespace std;

void setHi();

string saySorry();

void printIN(string name);


string getMsg(string msg);

  • stu3.cpp

    #include <iostream>
    #include "stu3.h"
    
    using namespace std;
    
    
    // 无参无返回值
    void setHi() {
    	cout << "你好···" << endl;
    }
    
    // 无参有返回值
    string saySorry() {
    	return "对不起了。。。";
    }
    
    // 有参无返回值
    void printIN(string name) {
    	cout << "我的名字是:" << name << endl;
    }
    
    // 有参有返回值
    string getMsg(string msg) {
    	return "我的消息是:" + msg;
    }
    
  • function.cpp

    #include <iostream>
    #include <string>
    #include "stu3.h"
    
    
    
    int main() {
    
    	setHi();
    	cout << saySorry() << endl;
    	printIN("小明");
    	cout << getMsg("你好") << endl;
    
    	return 0;
    }
    
    4.函数重载
    #include <iostream>
    
    using namespace std;
    
    int add(int a,int b) {
    	return a + b;
    }
    
    int add(int a, int b, int c) {
    	return a + b + c;
    }
    
    int add(double a, int b) {
    	return a + b;
    }
    
    int add(int a, double b) {
    	return a + b;
    }
    
    int main() {
    	cout << add(1, 3) << endl;
    	cout << add(1, 2, 3) << endl;
    	cout << add(1.2, 3) << endl;
    	cout << add(1, 1.2) << endl;
    }
    
    • 重载的分类:
      • 参数的个数不同
      • 参数的传参类型不同
      • 参数的传参类型顺序不同

相关推荐

  1. 函数总结1

    2024-05-02 01:04:01       34 阅读
  2. 函数指针一点总结

    2024-05-02 01:04:01       36 阅读
  3. MATLAB --- interp1( )函数用法

    2024-05-02 01:04:01       58 阅读
  4. python中函数运用(1)

    2024-05-02 01:04:01       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-02 01:04:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-02 01:04:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-02 01:04:01       82 阅读
  4. Python语言-面向对象

    2024-05-02 01:04:01       91 阅读

热门阅读

  1. WebView

    2024-05-02 01:04:01       28 阅读
  2. leetcode 2639.查询网格图种每一列的宽度

    2024-05-02 01:04:01       35 阅读
  3. 自制英语听力视频 5.1

    2024-05-02 01:04:01       31 阅读
  4. 2021江苏省赛 H-Reverse the String

    2024-05-02 01:04:01       31 阅读
  5. 【QEMU系统分析之实例篇(五)】

    2024-05-02 01:04:01       29 阅读
  6. 字节-隐私安全实习生

    2024-05-02 01:04:01       33 阅读
  7. PHP深入探索:面向对象编程与设计模式实战

    2024-05-02 01:04:01       36 阅读
  8. 数据结构之树形结构

    2024-05-02 01:04:01       36 阅读
  9. bbPress 中文汉化包,WordPress插件

    2024-05-02 01:04:01       32 阅读