正文

C++个人回顾小结: 函数(二)&&个人实践小结2006-11-26 17:15:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/edwardguo/20835.html

分享到:

函数的作用域

作用域的种类:

程序级:所有文件。外部函数。外部变量。

文件级:内部函数 外部静态类变量

函数级:函数的形参 函数内定义的自动类变量 内部静态类变量  语句标号

块级:定义在函数体内的分程序中、if、switch以及循环语句当中

关于重新定义标识符的作用域范围,例:

void fun()

{

    int a;

    {

        float a;

    }

}

int 型变量a在整个函数fun内都是有效的、可见的。而float型变量a仅在定义它的分程序内是可见的,在该分程序前和后的函数体内是不可见的。

关于一个标识符的可见性和存在性,有时是一致的,例如上例中的float型变量a;有时是不一致的,例如上例中的int型变量a,它在分程序内虽然不可见的,但是它是存在的。

举例:

#include<iostream.h>
void main()
{
 int a(5),b(7),c(10);
 cout<<a<<","<<b<<","<<c<<endl;
 {
  int b(8);
  float c(8.8);
  cout<<a<<","<<b<<","<<c<<endl;
  a=b;
  {
   int c;
   c=b;
   cout<<a<<","<<b<<","<<c<<endl;
  }
  cout<<a<<","<<b<<","<<c<<endl;
 }
 cout<<a<<","<<b<<","<<c<<endl;
}
输出:

5,7,10

5,8,8.8

8,8,8

8,8,8.8

8,7,10

实例2:

#include<iostream.h>

void main()

{

     int x(3);

    for(;x>0;x--)

    {

        int x(5);

      cout<<x<<"\t";

    }

    cout<<endl<<x<<endl;

}

 

局部变量:

作用在块级和函数级的变量。包含有自动类变量和内部静态变量以及函数参数。

内部静态变量的作用域和自动变量相同,但是生存周期是长的。可见性与存在性不一致。

例子:

#include<iostream.h>
void other();
void main()
{
 int a(3);
 register int b(5);
 static int c;
 cout<<"a="<<a<<","<<"b="<<b<<","<<"c="<<c<<endl;
 other();
 other();
}

void other()
{
 int a(5);
 static int b(12);
 a+10;
 b+=20;
 cout<<"a="<<a<<","<<"b="<<b<<endl;
}

输出:

a=3, b=5, c=0;

a=5, b=32

a=5, b=52

实例3:

该程序包含三个文件。

文件1(main.cpp)

#include<iostream.h>
void fun1(),fun2(),fun3();
int i(5);
void main()
{
 int i=20;
 fun1();
 cout<<"main():i="<<i<<endl;
 fun2();
 cout<<"main():i="<<i<<endl;
 fun3();
 cout<<"main():i="<<i<<endl;
}

文件file1.cpp

#include<iostream.h>
static int i;
void fun1()
{
 i=50;
 cout<<"fun1():i(static)="<<i<<endl;
}

文件file2.cpp

#include<iostream.h>
void fun2()
{
 int i=15;
 cout<<"fun2():i(auto)="<<i<<endl;
 if(i)
 {
  extern int i;
  cout<<"fun2():i(extern)="<<i<<endl;
 }
}
extern int i;
void fun3()
{
 i=30;
 cout<<"fun3():i(extern)="<<i<<endl;
 if(i)
 {
  int i=10;
  cout<<"fun3():i(auto)="<<i<<endl;
 }
}
输出:

fun1():i(static)=50

main():i=20

fun2():i(auto)=15

fun2():i(extern)=5

main():i=20

fun3():i(extern)=30

fun3():i(auto)=10

main():i=20

内部函数和外部函数:

1、内部函数:

内部函数是在定义它的文件中可以被调用的函数,而在同一程序的其他文件中不可以调用。

static <类型说明> <函数名> (<参数表>)

{

    <函数体>

}

程序示例:

#include<iostream.h>
int i(10);
static int reset(),next(int),last(int),other(int);
void main()
{
 int i=reset();
 for(int j(1);j<=3;j++)
 {
  cout<<i<<","<<j<<",";
  cout<<next(i)<<",";
  cout<<last(i)<<",";
  cout<<other(i+j)<<endl;
 }
}
static int reset()
{
 return i;
}

static int next(int j)
{
 j=i++;
 return j;
}

static int last(int j)
{
 static int i(20);
 j=i--;
 return j;
}

static int other(int i)
{
 int j(15);
 return i=j+=i;
}

输出:

10,1,10,20,26

10,2,11,19,27

10,3,12,18,28

程序实例:

文件f1.cpp

#include<iostream.h>
int i(1);
extern int reset(),next(),last(),other(int);
void main()
{
 int i=reset();
 for(int j(1);j<=3;j++)
 {
  cout<<i<<","<<j<<",";
  cout<<next()<<",";
  cout<<last()<<",";
  cout<<other(i+j)<<endl;
 }
}

文件f2.cpp

static int i(10);
extern int next()
{
 return i+=1;
}

extern int last()
{
 return i-=1;
}

extern int other(int i)
{
 static int j(5);
 return i=j+=1;
}

文件f3.cpp

extern int i;
extern int reset()
{
 return i;
}

输出:

1,1,11,10,6

1,2,11,10,7

1,3,11,10,8

阅读(4140) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册