函数的四种形式
函数定义和声明分开
函数分离式编译
函数重载
函数传递引用
内联函数
静态变量
sub01.h
#ifndef MYTEST_SUB01_H
#define MYTEST_SUB01_H
void sub1(int a,int b);
#endif //MYTEST_SUB01_H
sub01.cpp
#include <iostream>
#include "sub01.h"
using namespace std;
void sub1(int a,int b){
cout << a + b << endl;
}
function.cpp
#include <iostream>
#include <string>
#include "sub01.h"
using namespace std;
//函数的四种形式
// 无参无返回值
void goodMorning(){
cout << "goodmoring"<< endl;
}
// 无参有返回值
string say_hi(){
return "hi";
}
// 有参无返回值
void sayhello(string name){
cout << "hello" << name << endl;
}
// 有参有返回值
string nice(string name){
return "nice you" + name;
}
// 函数定义和声明分开
string getName(string name);
//函数分离式编译
// 函数重载
int add(int a,int b){
return a + b;
}
int add(int a,int b,int c){
return a + b + c;
}
int add(int a,double b){
return a +b;
}
// 函数传递引用
void getScore(int &score){
score = 100;
}
// 内联函数
inline int compare(int a,int b){
if(a > b){
return a;
}
return b;
}
//静态变量
void static_num(){
static int num =100;
num += 1;
cout << num<< endl;
}
int main(){
goodMorning();
string result1 = say_hi();
cout << result1 << endl;
sayhello("xiaohong");
string result2 = nice("xiaomli");
cout << result2 << endl;
string name = getName("xiaoming");
cout << name << endl;
sub1(1,2);
add(1,2);
add(1,2,3);
add(1,1.3);
int a = 10;
getScore(a);
cout << a << endl;
compare(1,2);
static_num();
static_num();
}
string getName(string name){
return name;
};