c++ 语法函数

函数定义

返回值 函数名(参数列表){

    函数体

    return xx

}

无返回值 不需要return 返回值类型为 void

int sum(int a,int b) {
    int sum = a + b;
    return sum;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
   int c = sum(10, 11);
    std::cout << c << std::endl;
    return 0;
}

值传递 函数的形参改变不会影响实参

void swap(int a, int b) {
    a = a + b;
    b = a - b;
    a = a - b;
    std::cout << "a=" << a << "b=" << b << std::endl;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int a = 12;
    int b = 10;
    swap(a, b);
    std::cout << "end a=" << a << "end b=" << b << std::endl;
    return 0;
}

函数类型

有参有返

int sum(int a,int b) {
    int sum = a + b;
    return sum;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
   int c = sum(10, 11);
    std::cout << c << std::endl;
    return 0;
}

有参无返

void swap(int a, int b) {
    a = a + b;
    b = a - b;
    a = a - b;
    std::cout << "a=" << a << "b=" << b << std::endl;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int a = 12;
    int b = 10;
    swap(a, b);
    std::cout << "end a=" << a << "end b=" << b << std::endl;
    return 0;
}

无参无返

void test(){
    std::cout << "no arg";
}

无参有返

int numb() {
    return  20;
}

函数声明 

注意函数声明可以有多次 但是定义只能有一次

// 声明
int max(int a, int b);
int main(int argc, const char * argv[]) {    
    int m = max(20, 15);
    std::cout << "max = " << m << std::endl;
    return 0;
}
//定义
int max(int a, int b) {
    return  a > b ? a : b;
}

函数分文件编写

1.创建后缀名为.h的头文件

2.创建后缀名为.cpp的源文件

3.在头文件中写函数的声明

4.在源文件中写函数的定义

头文件 mathutil.hpp

//
//  mathutil.hpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#ifndef mathutil_hpp
#define mathutil_hpp

#include <stdio.h>
void swap(int a, int b);
int sum(int a,int b);
#endif /* mathutil_hpp */

源文件 mathutil.cpp

//
//  mathutil.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//
#include <iostream>
#include "mathutil.hpp"
void swap(int a, int b) {
    a = a + b;
    b = a - b;
    a = a - b;
    std::cout << "a=" << a << "b=" << b << std::endl;
}

int sum(int a,int b) {
    int sum = a + b;
    return sum;
}

调用

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include <iostream>
#include "mathutil.hpp"

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int c = sum(10, 11);
    std::cout << c << std::endl;
    
    int a = 12;
    int b = 10;
    swap(a, b);
    std::cout << "end a=" << a << "end b=" << b << std::endl;
    
    int m = max(20, 15);
    std::cout << "max = " << m << std::endl;
    return 0;
}

函数默认参数

返回值类型 函数名(参数=默认值) {}

 注意 如果某个位置起有默认值 后边的参数都要有默认值

         函数声明和实现只能一个有默认参数 不能同时有

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include <iostream>
#include "mathutil.hpp"
#include <string>
using namespace std;
// 注意 如果某个位置起有默认值 后边的参数都要有默认值
// 函数声明和实现只能一个有默认参数 不能同时有
int func_test2(int a, int b=3, int c=2);
int func_test(int a, int b, int c) {
    return  a + b + c;
}
int func_test1(int a, int b = 20, int c = 30) {
    return  a + b + c;
}
int func_test2(int a, int b, int c){
    return  a + b + c;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    std::cout << func_test(10, 1, 22) << std::endl;
    
    std::cout << func_test1(22,44) << std::endl;
    return 0;
}

占位参数

语法 

返回值类型 函数名(参数类型1,参数类型2){}

#include <iostream>
#include "mathutil.hpp"
#include <string>
using namespace std;
// 占位参数
void func_test4(int a,int){
    std::cout << "func_test4!\n";

}
// 占位参数也可以有默认参数
void func_test5(int a,int = 20){
    std::cout << "func_test4!\n";

}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    func_test4(109, 22);
    return 0;
}

函数重载

同一个作用域下

函数名相同

函数的参数类型不同或者函数参数的个数不同或者顺序不同

但是函数的返回值不可以作为重载条件

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include <iostream>
#include "mathutil.hpp"
#include <string>
using namespace std;
void test_func(){
    std::cout << "test_func!\n";
}
void test_func(int a){
    std::cout << "test_func(int a)\n";
}
void test_func(int a,int c){
    std::cout << "test_func(int a,int c)\n";

}
//void test_func(int b,int a){
//    std::cout << "test_func(int b,int a)\n";
//
//}
void test_func(int a,double c){
    std::cout << "test_func(int a,double c)\n";

}
void test_func(double a,int c){
    std::cout << "test_func(double a,int c)\n";

}

void test_func(double a){
    std::cout << "test_func(double a)\n";
}
void test_func(float a){
    std::cout << "test_func(float a)\n";
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    test_func();
    test_func(10);
    test_func(11.90,2);
    test_func(11.2222222222222222);
    test_func(22.4);
    return 0;
}

注意

 引用

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include <iostream>
#include "mathutil.hpp"
#include <string>
using namespace std;
void test_func1(int &a) {//引用必须引用合法的内存空间
    std::cout << "test_func1(int &a)\n";

}
void test_func1(const int &a) {//const int a = 10;
    std::cout << "test_func1(const int &a)\n";
}


int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int a = 10;
    test_func1(a);//test_func1(int &a) // 引用必须引用合法的内存空间
    test_func1(10);//test_func1(const int &a) // 相当于 const int a = 10;
    return 0;
}

 默认参数

//
//  main.cpp
//  cpplearn
//
//  Created by KING on 2024/2/1.
//

#include <iostream>
#include "mathutil.hpp"
#include <string>
using namespace std;
void test_func2(int a) {
    std::cout << "test_func2(int a)\n";

}
void test_func2(int a, int b = 10) {
    std::cout << "test_func2(int a)\n";

}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    int a = 10;
    //test_func2(a);//函数重载遇到默认参数的时候会遇到 二义性 尽量避免这种情况
    return 0;
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值