052.Static Extern 内部函数与外部函数

来源:互联网 发布:世界上有鬼吗 知乎 编辑:程序博客网 时间:2024/06/05 03:55
---------------  main.m  ---------------
#import<Foundation/Foundation.h>

intmain()
{
   voidprintRect(int,int);
   void printTriangle(int);
   printRect(5,10);
   
printTriangle(3);
}

---------------  draw.m  ---------------
#import<Foundation/Foundation.h>

externvoidprintRect(intheight, int width)
{
   
for (inti = 0; i < height; i ++)
    {
       
for (intj = 0; j < width; j++)
        {
           
printf("*");
        }
       
printf("\n");
    }
}
externvoidprintTriangle(intheight)
{
   
for (inti = 0; i < height; i ++)
    {
       
for (intj = 0; j < height - 1 - i; j++)
        {
           
printf(" ");
        }
       
for (intj = 0 ; j < 2 * i + 1 ; j++)
        {
           
printf("*");
        }
       
printf("\n");
    }
}

一、编写本节代码的具体步骤:
1.打开Xcode。
2.点击Create a new Xcode project。
3.选择OS X下的Application,然后点击Command Line Tool,点next。
4.填写项目名称,机构名称和机构标志,选择Language是Objective-C,点next。
5.选择文件储存目录,不要勾选Create Git repository on My Mac,点Create。
6.选中左侧导航栏中的main.m文件,在右侧代码编辑区,编写代码如上。
7.右击左侧导航栏中的main.m,选择New File,选择OS X下的Source,
8.然后选择Objective-C File,填写文件名draw,选择类型为Empty File,点next,点Create。
9.选中左侧导航栏中的draw.m文件,在右侧代码编辑区,编写代码如上。

二、本节代码涉及到的知识点:
1.内部函数:定义时,使用static修饰,该函数只能被当前源文件中的其它函数调用。
  外部函数:定义时,使用extern修饰,该函数可以被任何源文件中的其它函数调用。
2.我们在定义函数时,可以选择使用extern或static,来决定这个函数是外部函数或内部函数。
3.通常我们省略不写extern,这意味着函数默认都是外部函数,extern被人们习惯性地省略了。
0 0