1.函数的概念:能够完成某些特定功能的独立代码段,称之为一个函数
2.函数实现了代码的可重用性,提高了程序的开发效率
3.函数的格式:
函数的格式:
函数的返回值类型 函数名(参数表)
{
函数体
}
4.c++不允许在函数定义里嵌套另一个函数的定义。
5.函数可以分为函数的声明和实现两部分。
6.函数的形参变量只有在被调用时才分配内存单元,在调用结束时释放所分配的内存单元。
7.函数的缺省参数又称为函数的默认参数或者可选参数。
8.当函数存在缺省参数时,所有的缺省参数必须都放在参数列表的最后,如以下格式:
返回值 函数名(必选参数1,.,.,必选参数n,可选参数1,.,.,可选参数n)
9.如以下代码:
//#include <iostream>
//using namespace std;
函数的定义格式:
///*
//返回值类型 函数名(参数表)
//{
////函数体
//}
//*/
//int getSum( int _a , int _b )
//{
//return _a + _b ;
//}
//
//int Test(int);//函数的声明,参数可以只给出类型,而不需要参数名
//
//bool playerIsDead(int _hp)
//{
//if(_hp <= 0)
//return true;
//else
//return false;
//}
//
//int WeekdayTest( int _iValue )
//{
//switch(_iValue)
//{
//case 0:
//cout<<"周日"<<endl;
//return _iValue;
//case 1:
//cout<<"周一"<<endl;
//break;
//}
//}
//
//void main()//C++语言的入口函数
//{
//反例:C++语言不支持在一个函数的内部定义另一个函数
// C++语言不支持函数的嵌套定义
////int getSum( int _a , int _b )
////{
////return _a + _b ;
////}
//int x = 0;
//int y = 0;
//cout<<"Input 2 Number :"<<endl;
//cin>>x>>y;
//
////函数的调用方式: 函数名(参数);
//int iSum = getSum(x,y);
//cout<<"iSum = "<<getSum(x,y)<<endl;
//
//Test(iSum);
//
//if( playerIsDead(-100) )
//cout<<"玩家挂了"<<endl;
//else
//cout<<"玩家活着..."<<endl;
//system("pause");
//}
函数的实现
//int Test( int _iValue )
//{
//cout<<"Test Func Called......"<<endl;
//cout<<_iValue<<endl;
//return 0;
//}
----------数组作为函数的参数----------
#include <iostream>
using namespace std;
void printArray( int _array[] , int _length )
{
if( _length < 5 )//若数组长度小于5,不进行处理,退出
return;
cout<<"-----------------------------"<<endl;
int iCount = 0;
for( int i = 0 ; i < _length ; ++i )
{
cout<<"iArray["<<i<<"] = "<<_array[i]<<endl;
++iCount;
if( iCount > 4 )//若次数iCount 大于4,结束处理,退出
return;
}
cout<<"aaaaaaaaaaa"<<endl;
}
void main()
{
int iArray1[10] = {87,654,3,4843,46,78,754,348};
printArray(iArray1,10);//输出5个值
int iArray2[20];// = { 1 };
printArray(iArray2,20);//输出5个值
system("pause");
}
//----------函数参数的传递---------
//#include <iostream>
//using namespace std;
//
//float getMax(float,float);
//
//void main()
//{
//int x = 13 , y = 5;
//float fTemp = 1.2f;
//cout<<getMax(x,y)<<endl;
//system("pause");
//}
//float getMax( float _a , float _b )
//{
//if( _a > _b )
//return _a;
//else
//return _b;
//}
10.函数重载的定义:所谓函数重载是指同一个函数名可以对应着多个函数的实现。
11.实现函数重载必须遵循两个原则:参数个数不同或者参数类型不同。
12.当函数仅返回值不同时构不成重载。
13.如果在两个函数的参数表中,只有可选参数不同,则第二个函数声明被视为第一个函数的重复声明。
14.仅仅靠修饰符const 标注的参数,如果参数的类型一样,则也构成重复声明。
15.当函数参数中出现const时,仅仅防止参数或者参数中的数据成员被改变。
16.函数参数的传递有两种形式:1为值传递2为按地址传递。
17.按值传递不会改变变量本身的值,按地址传递是对变量本身进行操作。
18.必须遵循形参不改变实参的值得原则。
19.代码如下:
//#include <iostream>
//using namespace std;
//#include <math.h>
------函数的缺省参数、可选参数、默认参数------
//void Test( int _a = 0 , int _b = 0 , int _c = 0 )
//{
//cout<<"a = "<<_a<<endl;
//cout<<"b = "<<_b<<endl;
//}
//int Test01( int _a , int _b = 0, int _c = 0 )
//{
//int iValue = 10;
//cout<<"a = "<<_a<<endl;
//cout<<"b = "<<_b<<endl;
//return iValue;
//}
//void getSum( int _a , double _b = pow(2.0,2) )
//{
//cout<<_a+_b;
//}
//
//void main()
//{
////当一个函数具有缺省参数的时候,我们对该函数的调用,如果
////明确的给出了实参,就用实参的值去替代形参的默认值,如果
////没有明确给出实参,就使用默认值对该形参赋值
//Test();//正确
//Test(10);//正确
//Test(100,200);//正确
////Test( 111, ,999 );//反例:错误的
////cout<<iValue<<endl;
////Test01();//反例:具有必选参数的函数,调用时必须至少将
////必须参数全部赋值
////Test01(1);//正确
////Test01(11111,22222); //正确
//cout<<"-------------"<<endl;
//int iValue = Test01(512);
//cout<<iValue<<endl;
//cout<<"-------------"<<endl;
//cout<<pow(2.0,12)<<endl;
//getSum(100,pow(2.0,12));
//system("pause");
//}
#include <iostream>
using namespace std;
//-------------函数的重载-------------
/*
函数之间能够构成重载的条件:
1、函数名完全相同
2、通过函数参数实现重载
1)参数的个数不同
2)参数的类型不同
*/
void TestFunc()
{
cout<<"无参函数TestFunc()被调用..."<<endl;
}
void TestFunc(int _iValue)
{
cout<<"有参函数TestFunc(int)被调用..."<<endl;
}
void TestFunc(float _fValue)
{
cout<<"有参函数TestFunc(float)被调用..."<<endl;
}
//void TestFunc(float _fValue = 1.0f)
//{
//cout<<"有参函数TestFunc(float _fValue = 1.0f)被调用..."<<endl;
//}
int getSum(int a,int b)
{
return a+b;
}
float getSum(float a,float b)
{
return a+b;
}
char getSum(char a,char b)
{
return a+b;
}
/* ?? 函数的返回值类型不同,能够构成重载吗 ??
void Test01()
{
}
int Test01()
{
return 0;
}
答案是:不行!!!!!
*/
void Test02( char a )
{
cout<<a<<endl;
}
void Test02( char a , int b = 10 )
{
cout<<a<<endl;
cout<<b<<endl;
}
//函数重载在游戏中的应用
void CreateRen()
{
int iHP = 1;
int iMP = 1;
int iLevel = 1;
cout<<"CreateRole()创建的角色:HP = "<<iHP<<" , MP = "<<iMP<<" , Level = "<<iLevel<<endl;
}
void CreateRen( int hp )
{
int iHP = hp;
int iMP = 0;
int iLevel = 1;
cout<<"CreateRole(int)创建的角色:HP = "<<iHP<<" , MP = "<<iMP<<" , Level = "<<iLevel<<endl;
}
void CreateRole( int hp , int mp )
{
int iHP = hp;
int iMP = mp;
int iLevel = 1;
cout<<"CreateRole(int,int)创建的角色:HP = "<<iHP<<" , MP = "<<iMP<<" , Level = "<<iLevel<<endl;
}
void CreateRole( int hp , int mp , int level )
{
int iHP = hp;
int iMP = mp;
int iLevel = level;
cout<<"CreateRole(int,int ,int)创建的角色:HP = "<<iHP<<" , MP = "<<iMP<<" , Level = "<<iLevel<<endl;
}
void main()
{
CreateRen();
CreateRole(10000000,10000000,100);
system("pause");
}
20.一些标准库函数:
21.对应的.h的文件:assert.h float.h math.h stdarg.h stdlib.h ctype.h limits.h setjmp.h stddef.h string.h errno.h locale.h signal.h stdio.h time.h
22.应用assert函数时必须包含assert.h文件。
23.math.h中的一些数学函数:
24.atof函数用于把字符串s转换成double类型,该函数等价于strtod。
25.atoi函数用于把字符串s转换成int类型,该函数等价于strtol。
26.atol函数用于把字符串s转换成long类型,该函数等价于strtol
27.memcpy函数用于把字符串src中的count个字符拷贝到dest中,并返回dest
void *memcpy(void* dest,const void* src,size_t count );
28.memmove函数的功能与memcpy相似,不同之处是当发生对象重叠时,该函数仍能正确执行。
29.memcmp函数用于把buf1的前count个字符与buf2相比较
int memcmp(const void* buf1, const void* buf2, size_t count );
如果buf1>buf2 则返回值为1,若相等则返回0,若小于则返回-1.
30.memset函数用于把dest中的前count个字符替换为c,并返回dest。
void *memset(void* dest, int c, size_t count );
31.strcpy函数用于把字符串strSource(包括'\0')拷贝到字符串strDestination中,并返回strDestination。
char *strcpy( char *strDestination, const char *strSource );
32.strncpy函数用于把字符串strSource中最多count个字符拷贝到字符串strDest中,并返回strDest
char *strncpy(char *strDest,const char *strSource,size_t count);
34.strcat函数用于把字符串strSource连结到strDestination的尾部,并返回strDestination。
char *strcat(char *strDestination, const char *strSource);
35.strncat函数用于把字符串strSource中最多count个字符连接到字符串strDest的尾部,并以'\0'结束,返回strDest
char *strncat(char *strDest, const char *strSource, size_t count );
36.strcmp函数用于比较字符串string1和string2;当string1<string2时它返回一个负值;当string1>string2时它返回一个正数;当string1== string2时它返回0。
int strcmp(const char *string1,const char *string2);
37.strncmp函数用于将字符串string1中最多count个字符与字符串string2相比较。当string1<string2时它返回一个负数;当string1>string2时它返回一个正数;当string1==string2时它返回0。
int strncmp(const char *string1,const char *string2,size_t count )
38.strlen函数用于返回字符串string的长度。
size_t strlen(const char *string );
39.strrchr函数用于返回一个指向字符串string中字符c最后一次出现的位置的指针。如果string中不包含c,那么函数返回NULL。
char *strrchr(const char *string,int c);
40.strspn函数用于返回字符串string中由字符串strCharSet中的字符构成的第一个子字符串的长度。
size_t strspn(const char *string, const char *strCharSet);
41.strcspn函数用于返回字符串string中由不在字符串strCharSet中的字符组成的第一个字符串的长度
size_t strcspn(const char *string,const char *strCharSet);
43.strpbrk函数用于返回指向字符串strCharSet中的任意字符第一次出现在字符串string中的位置的指针。
char *strpbrk(const char *string,const char *strCharSet);
44.strstr函数用于返回指向字符串strSearch第一次出现在字符串string中的位置的指针。
char *strstr(const char *string,const char *strSearch );
45.递归函数的定义:直接或间接调用自己的函数被称为递归函数。这种调用方式被称之为函数的递归调用。
46.递归函数必须定义一个停止条件,否则函数会永远递归下去,这被称作无限递归错误。
47.代码如下图:
//#include <iostream>
//using namespace std;
//
//int Test(int _iValue)//被调函数
//{
//int iTemp = _iValue;
//cout<<iTemp<<endl;
//_iValue = 200;
//return iTemp+10;
//}
常量作为函数参数特点是:
无法在函数内部对该形参的值进行修改,该参数在函数内部变为
了“只读型参数”
//int Test01( const int _iValue )
//{
//cout<<_iValue<<endl;
////_iValue = 200;//反例:无法对常量参数进行修改
//return 0;
//}
//#include <math.h>
//void main()//主调函数
//{
//cout<<Test(10)<<endl;//函数调用
////cout<<_iValue<<endl;
////cout<<iTemp<<endl;
//Test01(45);
//system("pause");
//}
#include <iostream>
using namespace std;
#define PI 3.1415926f
#include <math.h>
void main()
{
//三角函数:根据角度求比值
cout<<sin(PI/6)<<endl;
cout<<cos(PI/3)<<endl;
cout<<tan(PI/4)<<endl;
//反三角函数:根据比值求角度
cout<<asin(0.5f)<<endl;
//cout<<acos()<<endl;
cout<<PI/6<<endl;
system("pause");
}
#include <iostream>
using namespace std;
Void main()
{
//将字符串转化为double数据
char str1[10] = "12.3";
double dResult = atof(str1);
cout<<"atof() : "<<dResult<<endl;
//将字符串转化为int数据
char str2[10] = "456";
int iResult = atoi(str2);
cout<<"atoi() : "<<iResult<<endl;
//将int数据转换为string
int iValue = 156;
char str3[10] = {};
_itoa_s(iValue,str3,10); // Destination:目标,Buffer:缓冲区
cout<<"_itoa_s() : "<<str3<<endl;
//--------内存拷贝函数 memcpy ----------
char src[20] = "HelloC++";
char dest[11] ="HelloWorld";
//memcpy(src,src+5,3);
//memcpy(src,src+2,6);
//cout<<"src = "<<src+5<<endl;
//cout<<"dest = "<<dest<<endl;
cout<<"src = "<<src<<endl;
memmove(src,src+2,6);
cout<<"memmove : src = "<<src<<endl;
//--------内存比较函数:memcmp ----------
char str4[10] = "ababe";
char str5[20] = "abadEfG";
int iCmp = memcmp(str4,str5,3);
if(iCmp > 0)
cout<<" str4 > str5 "<<endl;
else if(iCmp == 0 )
cout<<" str4 == str5 "<<endl;
else
cout<<" str4 < str5 "<<endl;
int iArray1[10] = {11,12,13,14};
int iArray2[10] = {10,13,15,14};
int iTemp = memcmp(iArray1,iArray2,3);
if(iTemp > 0)
cout<<" iArray1 > iArray2 "<<endl;
else if(iTemp == 0 )
cout<<" iArray1 == iArray2 "<<endl;
else
cout<<" iArray1 < iArray2 "<<endl;
//--内存设置函数:memset,经常用于对内存空间的清空操作--
int iArray3[20];
for(int i = 0 ; i < 20 ; ++i)
cout<<iArray3[i]<<endl;
memset( iArray3 , -1 , 20*sizeof(int) );
for(int i = 0 ; i < 20 ; i++)
cout<<iArray3[i];//<<endl
//-----字符串拷贝函数strcpy-----
char str6[11] = "HelloWorld";
char str7[12] = {};
cout<<strcpy_s(str7,str6)<<endl;
cout<<str7<<endl;
//-----字符串拷贝函数strncpy-----
char str8[12] = {};
strncpy_s(str8,str6+5,5);
cout<<str8<<endl;
//-----字符串连接函数 strcat -----
char str9[6] = "World";
char str10[12] = "Hello";
strcat_s(str10,str9);
cout<<str10<<endl;
//--------------------------------------
/*
字符查找函数strrchr:
从一个字符串中查找最后一个出现的字符,
并返回指向该字符的指针
字符查找函数strchr:
从一个字符串中查找第一个出现的字符,
并返回指向该字符的指针
*/
char cPath[100] = "G:\\Resources\\Texture\\Hero.png";
char * pTarget = NULL;
pTarget = strrchr(cPath,'.');
if( strcmp(pTarget+1 , "jpg") == 0 )
cout<<"jpg Texture"<<endl;
else if( strcmp(pTarget+1 ,"bmp") == 0 )
cout<<"bmp Texture"<<endl;
else if( strcmp(pTarget+1 ,"png") == 0 )
cout<<"png Texture"<<endl;
//字符串长度函数:strlen(不包含'\0')
char string [20] = "HelloWorld";
cout<<strlen(string)<<endl;
cout<<sizeof(string)<<endl
system("pause");
}
//#include <iostream>
//using namespace std;
//void putValues(int _iValue)
//{
//if( _iValue < 10 )
//{
//cout<< _iValue++<<endl;
//putValues(_iValue);
//}else
//{
//return;
//}
//}
//void main()
//{
//putValues(4);
//system("pause");
//}