c++基础篇6(函数)

1.声明、定义和使用函数

#include <iostream>
using namespace std;
void show();      //函数声明语句
void main()
{
	show();       //函数调用
	system("pause");
}
void show()       //函数定义
{
	cout << "爱你,胖胖" << endl;
}

结果:
在这里插入图片描述

2.调用默认参数的函数,
实例输出两行字符串,一行是函数默认参数,一行是通过传字符串实参。

#include <iostream>
using namespace std;
void show(const char* Data = "Pangpang,I love you")
{
	cout << Data << endl;
}
void main()
{
	show();                             //默认
	show("Pangpang,I miss you");        //直接
	system("pause");
}

结果:
在这里插入图片描述

3.定义省略号形式的函数参数

#include <iostream>
#include <STDARG.H>
using namespace std;
void Outputinfo(int num,...)
{
	va_list argument;
	va_start(argument, num);
	while (num--)
	{
		char* pchData = va_arg(argument, char*);
		int iData = va_arg(argument, int);
		cout << pchData << endl;
		cout << iData << endl;
	}
	va_end(argument);
}
void main()
{
	Outputinfo(2, "B", 2008, "O", 2008);
	system("pause");
}

结果:
在这里插入图片描述

4.函数调用
传值调用、嵌套调用和递归调用
5.使用传值调用

#include <iostream>
using namespace std;
void swap(int a,int b)
{
	int tmp;
	tmp = a;
	a = b;
	b = tmp;
}
void main()
{
	int x, y;
	cout << "输入两个数:" << endl;
	cin >> x;
	cin >> y;
	if (x < y)
	{
		swap(x, y);
	}
	cout << "x="<<x<< endl;
	cout << "y="<<y<< endl;
	system("pause");

}

结果:
在这里插入图片描述
并不能实现

6.嵌套调用

#include <iostream>
using namespace std;
void Showlove()
{
	cout << "I love you,Pangpang" << endl;
}
void Zong()
{
	Showlove();
}
void main()
{
	Zong();
	system("pause");

}

结果:
在这里插入图片描述

7.递归调用
求n的阶乘

#include <iostream>
using namespace std;
int show(int n)
{
	if (n == 0)
	{
		return 1;
	}
	else
	{
		return n*show(n - 1);
	}
		

}

void main()
{
	int n;
	cout << "Please input a number:" << endl;
	cin >> n;
	cout << show(n);
	system("pause");

}

结果:
在这里插入图片描述

8利用循环求你n的阶乘。

#include <iostream>
using namespace std;
int show(int n)
{
	int i,j=1;
	for ( i = 1; i <= n;i++)
	{
		j*=i;
	}
	return j;
}
void main()
{
	int n;
	int f;
	cout << "Please input a number:" << endl;
	cin >> n;
	f=show(n);
	cout << f;
	system("pause");

}

结果:
在这里插入图片描述

10.重载函数

#include <iostream>
using namespace std;               
//int Add();
//double Add();                 //重载时无法声明(不知道为什么)
int Add(int a, int b)
{
	cout << "int add" << endl;
	return a + b;
}
double Add(double a, double b)
{
	cout << "double add" << endl;
	return a + b;
}
void main()
{
	int x = Add(5, 2);
	float y = Add(10.5, 11.4);
	system("pause");

}



结果:
在这里插入图片描述

11.内联函数
只起优化作用

#include <iostream>
using namespace std;
inline int IntegerAdd(int x, int y);
void main()
{
	int a=1;
	int b=1;
	int result= IntegerAdd(a,b);
	system("pause");

}
int IntegerAdd(int x, int y)
{
	return x + y;
}



12.变量的存储类别
auto变量、static变量、register变量、extern变量

使用static变量实现累加

#include <iostream>
using namespace std;
int add(int x)
{
	static int n = 0;
	n = n + x;
	return n;
}
void main()
{
	int i, j, sum;
	cout << "input the number:" << endl;
	cin >> i;
	cout << "the result is:" << endl;
	for (j = 1; j <= i; j++)
	{
		sum = add(j);
		cout << j << ":" << sum << endl;
	}
	system("pause");

}



结果:
在这里插入图片描述

#include <iostream>
using namespace std;
int add(int x)
{
	int n = 0;
	n = n + x;
	return n;
}
void main()
{
	int i, j, sum;
	cout << "input the number:" << endl;
	cin >> i;
	cout << "the result is:" << endl;
	for (j = 1; j <= i; j++)
	{
		sum = add(j);
		cout << j << ":" << sum << endl;
	}
	system("pause");

}



结果:
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值