天津城市建设学院:《C程序设计语言》 Chapter 8 Functions

The C Programming language Chapter8 Functions Chapter 8 Functions 口 Overview 口 Function definition 口 The return statement U Arguments -- Call by value 头 a Function Invocation a Nested invocation and Recursion 日 arrays as parameters u Storage classes of variables
The C Programming Language Chapter 8 Functions Chapter 8 Functions ❑ Overview ❑ Function Definition ❑ The Return Statement ❑ Arguments --- Call By Value ❑ Function Invocation ❑ Nested invocation and Recursion ❑ Arrays as parameters ❑ Storage classes of variables

The c Programming language Chapter8 Functions 88.1 Overview Modular programming > Taking a problem and breaking it into small, manageable pieces is critical to writing large problems >In C, the function construct is used to implement this top-down" method of programming a program has and only has one main( function L! Program execution begins and finishes with main A Functions are defined as individual objects that can not be nested. But they can call each other Preprocessing directives Function 1 Function n Declaration Statements Structure of C program
The C Programming Language Chapter 8 Functions §8.1 Overview ❖ Modular programming ➢Taking a problem and breaking it into small, manageable pieces is critical to writing large problems. ➢In C, the function construct is used to implement this “top-down” method of programming. File 1 Preprocessing directives Declarations Statements Function 1 Function n File i File n C program Structure of C program A program has and only has one main() function; Program execution begins and finishes with main; Functions are defined as individual objects that can not be nested.But they can call each other

The C Programming language Chapter8 Functions ☆ Sorts of functions From the point of view of users Standard function (library function) provided by system Function writed by users From the function format Function with parameters Function without parameters
The C Programming Language Chapter 8 Functions ❖ Sorts of functions ➢ From the point of view of users ▪ Standard function (library function): provided by system ▪ Function writed by users ➢ From the function format ▪ Function with parameters ▪ Function without parameters

The c Programming language Chapter8 Functions Returne-type by the function If omitted, then it is int by default. If no value is returned, then the type is void Valid identifier Newer style:type function-name( parameter declaration list) declarations statements Function body as function without parameters printstar() print(“******n”) O printstar(void { printf(***n”);}
The C Programming Language Chapter 8 Functions §8.2 Function Definition type function-name( parameter declaration list ) { declarations statements } Newer style: as function with parameters int max( int x , int y ) { int z; z=x>y?x:y; return(z); } as function with parameters int max(int x, y) { int z; z=x>y?x:y; return(z); } as do-nothing function dummy( ) { } is useful as a place holder during program development as function without parameters printstar( ) { printf(“**********\n”); } or printstar(void ) { printf(“**********\n”); } Function body Valid identifier Returne-type by the function If omitted,then it is int by default. If no value is returned, then the type is void

The C Programming language Chapter8 Functions Traditional style type function-name( parameter list parameter declarations declarations statements as function with parameters int max(x y) Int x,y int Z zx>y?xy return(z)
The C Programming Language Chapter 8 Functions type function-name( parameter list ) parameter declarations { declarations statements } Traditional style: as function with parameters int max(x,y) int x,y; { int z; z=x>y?x:y; return(z); }

The c Programming language Chapter8 Functions s8.3 The Return Statement o Format: return( expression ) O return expression O return Function The return statement is the mechanism for returning a value from the called function to the calling function ☆Spec >Th as function without returned value ts in a function >If void swap(int x, int y is passed back toi int temp g brace)is en temp-=X sary to the y=temp >Be value
The C Programming Language Chapter 8 Functions §8.3 The Return Statement ❖ Format: return( expression ); or return expression ; or return ; ❖ Function: The return statement is the mechanism for returning a value from the called function to the calling function. ❖ Specifications: ➢There can be zero or more return statements in a function; ➢If there is no return statement,then control is passed back to the calling environment when the closing brace } is encountered; ➢The expression will be converted, if necessary, to the return type of the function . ➢Be notice to the functions needn’t returned value. as function without returned value void swap(int x,int y ) { int temp; temp=x; x=y; y=temp; }

The C Programming language Chapter8 Functions Exp: return garbage printstar( void printstar( printf(求**) printf("****"); mair main( Int a: int a a=printstarO a=printstar( printf("%/od", a) printf("%d", a) output: 10 Compiling error
The C Programming Language Chapter 8 Functions printstar() { printf("**********"); } main() { int a; a=printstar(); printf("%d",a); } Exp : return garbage output:10 void printstar() { printf("**********"); } main() { int a; a=printstar(); printf("%d",a); } Compiling error!

The c Programming language Chapter8 Functions Exp: The expression will be converted to the return type of the function as specified in the function definition mainO i float a, b Int c scanf( %of, %of", &a, &b) c=max(a, b) printf("Max is%dn",c) int max(float x, float y) i float z y!xy, return(z)
The C Programming Language Chapter 8 Functions Exp : The expression will be converted to the return type of the function as specified in the function definition . main() { float a,b; int c; scanf("%f,%f",&a,&b); c=max(a,b); printf("Max is %d\n",c); } int max(float x, float y) { float z; z=x>y?x:y; return(z); }

The c Programming language Chapter8 Functions 8.4 Arguments - Call By Value B Parameter(formal argument)and Argument(actual argument parameter: variables named in the parenthesized list in a function definition >argument: the value used in a call of the function Exp: compare two numbers maino i int a, b c=max(a, b:(main )scanf("%d, %od", &a, &b); arguments max(int x, int y) (max万1c= max(a, b) printf("Max is %d", c); i int z z=>y?: y max(int x, int y parameters returni int return(
The C Programming Language Chapter 8 Functions §8.4 Arguments --- Call By Value ❖Parameter(formal argument) and Argument (actual argument) ➢parameter :variables named in the parenthesized list in a function definition; ➢argument : the value used in a call of the function. c=max(a,b); (main ) max(int x, int y)(max ) { int z; z=x>y?x:y; return(z); } Exp:compare two numbers and output the max main() { int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("Max is %d",c); } max(int x, int y) { int z; z=x>y?x:y; return(z); } arguments parameters

1 The C Programming Language Chapter8 Functions ☆ Specifications: Arguments must have definit value > Type of the parameter must be specified in the function definition >typically, the arguments match in number and type the parameters K >The arguments will be converted, if necessary, to the type of the parameters >The called function is given the values of its arguments in temporary variables rather than the originals. So the called function cannot directly alter a variable in the calling function. Call-By-Value)
The C Programming Language Chapter 8 Functions ❖ Specifications: ➢Arguments must have definit value; ➢Type of the parameter must be specified in the function definition; ➢Typically,the arguments match in number and type the parameters; ➢The arguments will be converted, if necessary, to the type of the parameters; ➢The called function is given the values of its arguments in temporary variables rather than the originals.So the called function cannot directly alter a variable in the calling function.(Call-By-Value)
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 天津城市建设学院:《C程序设计语言》 Chapter 7 Arrays.ppt
- 天津城市建设学院:《C程序设计语言》 Chapter 5 Control Flow.ppt
- 天津城市建设学院:《C程序设计语言》 Chapter 4 Input and Output.ppt
- 天津城市建设学院:《C程序设计语言》 Chapter 3 Data types, Operators, and Expressions.ppt
- 天津城市建设学院:《C程序设计语言》 第二章 算法 algorithm.ppt
- 天津城市建设学院:《C程序设计语言》 第十二章 文件.ppt
- 天津城市建设学院:《C程序设计语言》 第十一章 结构体与共用体.ppt
- 天津城市建设学院:《C程序设计语言》 Chapter 1 An Overview of c.ppt
- 《计算机导论》课程教学资源(PPT课件)第四章 高级语言软件开发能力培养.ppt
- 《计算机导论》课程教学资源(PPT课件)第三章 计算机操作能力培养.ppt
- 《计算机导论》课程教学资源(PPT课件)第七章 计算机网络能力培养.ppt
- 《计算机导论》课程教学资源(PPT课件)第六章 多媒体应用能力培养.ppt
- 《计算机导论》课程教学资源(PPT课件)第五章 信息系统开发能力培养.ppt
- 《计算机导论》课程教学资源(PPT课件)第二章 计算机硬件能力培养.ppt
- 《计算机导论》课程教学资源(PPT课件)第一章 计算机基础知识.ppt
- 《VisuaI Basic程序设计教程》 编程题答案.doc
- 《VisuaI Basic程序设计教程》 课程介绍.ppt
- 《VisuaI Basic程序设计教程》 教学安排.doc
- 《VisuaI Basic程序设计教程》 教学大纲.doc
- 《VisuaI Basic程序设计教程》 第三章 课堂讨论.ppt
- 天津城市建设学院:《C程序设计语言》 第九章 预处理命令.ppt
- 天津城市建设学院:《C程序设计语言》 曲型考题.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第一章 An Overview of C(1.2)The feature of C.ppt
- 中国科学技术大学:《计算机安全》课程教学资源(教案讲义)第一章 准备.doc
- 中国科学技术大学:《计算机安全》课程教学资源(教案讲义)第六章 Unix的安全.doc
- 中国科学技术大学:《计算机安全》课程教学资源(教案讲义)第十四章 数据库安全.doc
- 中国科学技术大学:《计算机安全》课程教学资源(教案讲义)第十一章 万维网WwW安全.doc
- 中国科学技术大学:《计算机安全》课程教学资源(教案讲义)第十章 分布式系统安全.doc
- 中国科学技术大学:《计算机安全》课程教学资源(教案讲义)第十二章 密码学介.doc
- 中国科学技术大学:《计算机安全》课程教学资源(教案讲义)第十三章 网络安全.doc
- 《Visual FoxPro程序设计》 第一章 Visual FoxPro基础.ppt
- 《Visual FoxPro程序设计》 第七章 数组.ppt
- 《Visual FoxPro程序设计》 第三章 表单设计与应用.ppt
- 《Visual FoxPro程序设计》 第九章 菜单和自定义工具栏.ppt
- 《Visual FoxPro程序设计》 第二章 Visual Foxpro程序设计基础.ppt
- 《Visual FoxPro程序设计》 第五章 选择结构程序设计.ppt
- 《Visual FoxPro程序设计》 第八章 自定义属性和方法.ppt
- 《Visual FoxPro程序设计》 第六章 循环结构程序设计.ppt
- 《Visual FoxPro程序设计》 第十一章 数据库及其操作.ppt
- 《Visual FoxPro程序设计》 第十三章 查询与视图.ppt