#C++ Day13 October 23 2025

//8 – 1 function

#include <iostream>

using namespace std;

/*

1.the type of return value:int,float,double,char,string ……

2.function name

3.parameter’s list:this function’s parameters that I need to deliver

4.return the expression(表达式)

返回值类型 函数名(参数列表)

{

函数体(各种要写的编程语句)

return 表达式

}

type   function name(parameter list)

{

body of function;

}

*/

int add(int a,int b)

{

int c = a + b;

return c;

}

double add2(int a, int b)

{

int c = a + b; //conversion from ‘double’ to ‘int’, possible loss of data

return c;

}

double add3(int a, int b)

{

int c = a + b; //conversion from ‘double’ to ‘int’, possible loss of data

return c;//this step would not be an error,but there is an unvisable translation

//return (double)c; small to big will not make a problem

}

int add4(int a, int b)

{

return a+b;

}

void printAaddB(int a, int b) //func type is null/void(空)

{

int c = a  + b ;

cout << c << endl;

return ; //Don’t need to return a value because it’s function type is null/void

}

int max(int a, int b)

{

if (a > b) {

return a;

}

return b;

//to get the maximum value

//or we can use ternary operator

// return a > b ? a : b;

}

int max2(int a, int b)

{

//or we can use ternary operator

return a > b ? a : b;

}

int main(){

//int ret = add2(1, 7);//conversion from ‘double’ to ‘int’, possible loss of data

double ret = add3(1, 7);//this step would not be an error,but there is an unvisable translation

cout << ret << endl;

return 0;

//int is the main function’s return date type

//0 is the main function’s return value

//main is the function’s name

//()is the function’s parameter

}

//8 – 2  function call

#include <iostream>

using namespace std;

/*

function’s calling have the use of encapsulation(封装)

//黑盒(Blach box):we don’t need to know how to get it

*/

int add(int a, int b) {

return a + b;

//return a – (-b);

}

int max(int a, int b) {

return a > b ? a : b;

}

int sum(int n) {

int ret = 0;

for (int i = 1; i <= n; ++i) {

ret += i;

}

//O(n)

return ret;

//枚举算法

//this method is much more calling than the sum2’s method

}

int sum2(int n) {

return (1+n) * n /2;

//O(1)

}

int main() {

//1.the addition’s call(加法的调用)

int a = add(1, 7);

int b = add(a,9);

int c = add(add(1, 7), 9);

//After made the function, I just need to use, instead of knew the details 

cout << b << endl;

cout << c << endl;

//2.the maximum number’s call(最大值的调用)

int d = max(a, b);

//3.to get the sum from 1 to n(求1到n的和)

int n;

cin >> n;

int e = sum2(n);

cout << e << endl;

return 0;

}

//8 – 3 function value transfer

#include <iostream>

using namespace std;

/*

| a | b   | | a   | | b   | |tmp | | a | | b  |

| 6 | 9   | | 6   | | 9   | | 6  | | 9 | | 6  |

4 bytes  4 bytes 4 bytes 4 bytes

*/

//function value transfer:transfer some parameters into the function inside, and use it within the function inside

void swap(int a, int b) {

cout << “a = ” << a << endl;

cout << “b = ” << b << endl;

cout << “————-“<< endl;

int tmp = a;

a = b;

b = tmp;

cout << “a = ” << a << endl;

cout << “b = ” << b << endl;

cout << “————-” << endl;

}

int main() {

int a = 6;

int b = 9;

swap(a,b);

//the key point of function value transfer:the value we transfered will only be copied, the origin value is the same address

//the value will be destroied when the function is finished execution

//If you want to swap the original value, you can use the pointer

cout << “a = ” << a << endl;

cout << “b = ” << b << endl;

return 0;

}

//8 – 4 function declaration

#include <iostream>

using namespace std;

//int add(int a, int b);//this is a function declaration

int add(int, int);//we can only write the data type when declarating

void func2(int);//declarating in advance is to prevent error

void func1(int x) {

if (x <= 0) {

return;

}

cout << “func1:” << x << endl;

func2(x-1);

}

void func2(int x) {

cout << “func2:” << x << endl;

func1(x-1);

}

int main() {

int x, y;

cin >> x >> y;

int z = add(x, y);

cout << z << endl;

cout << “———” << endl;

func1(-1);

return 0;

}

int add(int a, int b) {

return a + b;

}

//Place the function in this position, we won’t be able to run the code normally,if we didn’t declarate the function at first