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;
    }
    函数重载
  • 相关阅读:
    java+opencv实现图像灰度化
    java实现高斯平滑
    hdu 3415 单调队列
    POJ 3368 Frequent values 线段树区间合并
    UVA 11795 Mega Man's Mission 状态DP
    UVA 11552 Fewest Flops DP
    UVA 10534 Wavio Sequence DP LIS
    UVA 1424 uvalive 4256 Salesmen 简单DP
    UVA 1099 uvalive 4794 Sharing Chocolate 状态DP
    UVA 1169uvalive 3983 Robotruck 单调队列优化DP
  • 原文地址:https://www.cnblogs.com/cou1d/p/14234823.html
Copyright © 2011-2022 走看看