《C程序设计语言》课程PPT教学课件(讲稿)Chapter 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程序设计语言》课程PPT教学课件(讲稿)Chapter 7 Arrays.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)Chapter 5 Control Flow.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)Chapter 4 Input and Output.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)Chapter 3 Data types, Operators and Expressions.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)Chapter 1 An Overview of c.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第二章 算法 algorithm.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)Chapter 1 An Overview of C(1.2)The feature of C.ppt
- 清华大学计算中心:《计算机程序设计基础》第五章 结构化程序设计概论.ppt
- 清华大学:《计算机程序设计基础》课程教学资源(PPT课件)第四章 复合数据结构基础.ppt
- 清华大学:《计算机程序设计基础》课程教学资源(PPT课件)第三章 程序控制结构.ppt
- 清华大学:《计算机程序设计基础》课程教学资源(PPT课件)第一章 C语言的基本概念(主讲:乔林).ppt
- 清华大学:《计算机程序设计基础》课程教学资源(PPT课件)第二章 基本数据类型及其运算.ppt
- 《C语言程序设计导论》课程电子教案(PPT教学课件)第四章 函数和程序结构.ppt
- 《C语言程序设计导论》课程电子教案(PPT教学课件)第三章 语句及控制结构.ppt
- 《C语言程序设计导论》课程电子教案(PPT教学课件)第二章 数据类型、运算符与表达式.ppt
- 《C语言程序设计导论》课程电子教案(PPT教学课件)第一章 程序设计概述.ppt
- 《C语言程序设计导论》课程电子教案(PPT教学课件)第十章 位运算.ppt
- 《C语言程序设计导论》课程电子教案(PPT教学课件)第五章 预处理命令.ppt
- 《C语言程序设计导论》课程电子教案(PPT教学课件)第九章 文件.ppt
- 《C语言程序设计导论》课程电子教案(PPT教学课件)第七章 指针.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第九章 预处理命令.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第十一章 结构体与共用体.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第十二章 文件.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)典型考题.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第四章 C语言简单程序设计.ppt
- 《C程序设计语言》课程教学资源(习题作业)作业-选择.xls
- 《C程序设计语言》课程PPT教学课件(讲稿)知识点回顾.ppt
- 《C程序设计语言》课程教学资源(习题作业)作业.xls
- 《C程序设计语言》课程PPT教学课件(讲稿)第5章 选择结构程序设计.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)知识点回顾.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)流程图.ppt
- 《C程序设计语言》课程教学资源(习题作业)作业.xls
- 《C程序设计语言》课程教学资源(习题作业)答案3.rtf
- 《C程序设计语言》课程PPT教学课件(讲稿)知识点回顾——If的使用.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)数据类型复习.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)选择程序举例.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)循环控制.ppt
- 《C程序设计语言》课程教学资源(习题作业)第8次作业(循环结构).rtf
- 《C程序设计》第1章 C语言概述.ppt
- 《C程序设计》第2章 数据类型、运算符与表达式(1 C语言的数据类型 2 常量和变量 3 整型数据 4 实型数据 5 字符型数据).ppt