zoukankan      html  css  js  c++  java
  • C++基础四-函数

    #include <iostream>
    #include <string>
    #include "mult.h"//引用函数
    
    using namespace std;
    
    /* 1.定义函数
    return_type function_name( parameter list )
    {
       body of the function
       return 
    }
    */
    //如果不需要返回值,用void 声明
    void add(int a1, int a2) {
        cout << "First num : " ;
        cin >> a1;
        cout << "Second num : ";
        cin >> a2;
        int sums = a1 + a2 ;
        cout << "Add  num = " << sums << endl;
        
    }
    int add2(int a1, int a2) {
        int sums = a1 + a2;
        cout << sums << endl;
        return sums;
    }
    
    /*
    2.函数分文件编写
        1-创建.h后缀名的头文件
        2-创建.cpp后缀名的源文件
        3-在头文件中写函数的声明
        4-在源文件中写函数的定义
    */
    
    
    int main() {
        add(0,0);
        add2(5, 5);
        mult(0,0);
        return 0;
    
    }
    入口
    #include <iostream>
    #include "mult.h"
    using namespace std;
    
    //函数定义
    void mult(int a1 ,int a2) {
        
        cout << "num 1 : " ;
        cin >> a1;
        cout << "num 2 : " ;
        cin >> a2;
        int all = a1 * a2;
    
        cout << "Mult is :" << all << endl;
    
    }
    mult.cpp
    #pragma once
    #include <iostream>
    using namespace std;
    //函数声明
    void mult(int a1, int a2);
    mult.h
    #include <iostream>
    using namespace std;
    
    #define MAX=100;
    
    
    //1.函数重载:函数名可以相同,提高复用性
    /*
    需在同一作用域下;
    函数名字相同;
    函数参数,个数,顺序不同;
    函数返回值不可以作为重载条件
    */
    
    void func() { cout << "func1" << endl; };
    
    void func(int a) { cout << "func2" << endl; }
    
    //int func(){return 22;} 报错
    
    int main() 
    {
        
        func();
        func(11);
        return 0;
    }
    函数重载
  • 相关阅读:
    试试主题显示
    四则运算
    实验四 决策树算法及应用
    实验三 朴素贝叶斯算法及应用
    实验二 K-近邻算法及应用
    实验一 感知器及其应用
    实验三 面向对象分析与设计
    实验二 结构化分析与设计
    实验一 软件开发文档与工具的安装与使用
    举例分析流程图与活动图的区别与联系
  • 原文地址:https://www.cnblogs.com/cou1d/p/14234823.html
Copyright © 2011-2022 走看看