1.函数有主函数(main)和一般函数之分,主函数可以调用一般函数,反之则不可以.
2.一般函数之间可以相互调用,如果直接或间接调用自身则为直接或间接递归.
3.函数返回值有三种:
1)void类型,只能作为函数语句使用
2)简单类型(如:int , int * 等等)或记录类型(如:student,person等等)
3)简单类型或记录类型的引用( 如: int&, student&等)
举例如下:
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
const int PN = 5;
struct pupil
{
char pnum[8]; //学号
int grade; //分数
};
//超前说明三个
重载函数的原型
int find(pupil b[],int n,char* k);
pupil* find(int n,pupil b[],char* k);
pupil
& find(char* k,pupil b[],int n);
void main()
{
pupil a[PN] = {{"010203",78},{"010204",92},{"020101",85},{"020301",63},{"040502",87}};
int i;
for(i = 0;i < PN;i++)
{
cout<<a[i].pnum<<" "<<a[i].grade<<endl;
}
cout<<endl;
pupil x = {"020101",98};
cout<<"请输入您的选择(1,2,3)?";
cin>>i;
switch(i)
{
case 1:
i = find(a, PN, x.pnum);
if (i < PN)
{
a[i] = x;
}
else
{
cout<<x.pnum<<"not found!"<<endl;
}
break;
case 2:
pupil* p;
p = find(PN, a, x.pnum);
if (p != NULL)
{
*p = x;
}
else
{
cout<<x.pnum<<"not found!"<<endl;
}
break;
case 3:
find(x.pnum,a,PN) = x ;
}
for(i = 0;i < PN;i++)
{
cout<<a[i].pnum<<" "<<a[i].grade<<endl;
}
}
int find(pupil b[],int n,char* k) //返回元素下标
{
for(int i = 0;i < n;i++)
{
if (strcmp(b[i].pnum,k) == 0)
{
return i;
}
}
return i;
}
pupil* find(int n,pupil b[],char* k)
{
for(int i = 0;i < n;i++)
{
if (strcmp(b[i].pnum,k) == 0)
{
return &b[i];
}
}
return NULL;
}
pupil& find(char* k,pupil b[],int n)
{
for(int i = 0;i < n;i++)
{
if (strcmp(b[i].pnum,k) == 0)
{
return b[i];
}
}
cerr<<k<<"not found!"<<endl;
exit(1);
}
转载于:https://www.cnblogs.com/zqblog007/archive/2008/10/14/1311139.html