c语言程序设计课件ppt(英文)c program language 之5 function and program structure_第1页
c语言程序设计课件ppt(英文)c program language 之5 function and program structure_第2页
c语言程序设计课件ppt(英文)c program language 之5 function and program structure_第3页
c语言程序设计课件ppt(英文)c program language 之5 function and program structure_第4页
c语言程序设计课件ppt(英文)c program language 之5 function and program structure_第5页
已阅读5页,还剩61页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

LOGOFunction and Program Structure Chapter Four ContentsBasics of Functions 1return statements2Local variables and externavariables3Recursion4Scope Rules 5Header Files6Static Variables7Register Variables89 The C PreprocessorFunction and Program Structure 老师让几个同学编程, A负责编写求两个数的最大公约数; B负责两个数的最小公倍数; C负责判断这两个数是不是亲密数; D负责计算这两个数中有多少既能被 3整除又能被 7整除的数 老师说出了两个数, A,B,C,D立即把自己程序运行的结果告诉老师;老师又说了另外两个数, A,B,C,D又给出新的结果 AB CDmain( )( int a,b; scanf(“%d,%d”,funa(a,b); funb(a,b); func(a,b); fund(a,b);36, 120Function and Program Structure Amain( ) int a,b,h;scanf(“%d,%d”,if(ab) a=a+b;b=a-b;a=a-b; for(h=a;h=2;h-)If( a%h=0 printf(“%d,%d,%d”,a,b,h);funa( int a , int b) int h ;if(ab) a=a+b;b=a-b; a=a-b; for(h=a;h=2;h-)If( a%h=0 printf(“%d,%d,%d”,a,b,h);return ;main( )( int a,b; scanf(“%d,%d”,funa(a,b); funb(a,b); func(a,b); fund(a,b);36, 120Function and Program Structure C program source1source2source3.sourcenThe C Preprocessorfunction1function2.functionnFunction is the smallest unit in C program.Function definitions can appear in any order,and in one source file or severalA program may reside in one or more source files. Source files may be compiled separately and loaded together, along with previously compiled functions from librariesFunctionDeclaration StatementExecutive StatementVariablesFunction prototypeVariables typeVariable nameBlank statementFunction call statementControl statementBlock structureExpression statementFunction and Program Structure A program is just a set of definitions of variables and functions. Communication between the functions is by arguments and values returned by the functions, and through external variables. DefinitionPrototypeCallReturn ArgumentsBasics of Functions1 The types of functions Type Standard functions( library)Need use head files#includeUser- defined functionsEach function definition has the form 2 Definition of a function return-type function-name ( argument declarations ) declarations statements The type of the result that the function returns parameter types and names If return type is omitted, int is assumed A minimal function is empty function,which does nothing and returns nothing .Basics of Functionsfunc( ) printf(“ * n”); main( ) int i;main( ) for(i=1; iy? x: y ; return( n ); Basics of Functionsinput: 6.8, 4.5 m=?there is no function declaration in caller.return type is float, the actual type of return value is int.float m;%freturn statement1 return statement return expression ; The return statement is the mechanism for returning a value from the called function to its caller. A function neednt return a value, the expression can be omitted, or no return statement.A return statement with no expression passes a no useful value to the caller . Sometimes it is a trouble.when defining function type to “void” , no any value returned .It is allowed that several return statements occur in a function.void function name( ) float f(float x, float y) float z; z=x+y*y;if( ! y) return(0.0);return(cos(z) );1. return(ab?a:b);2. return(0);3. return (x);4. retrun;5. no return statement6. void return statementmain( ) int a;void disp( );printf (“Enter an integer:n”);scanf (“ % d ”, disp( a );If y is zero, return 0Otherwise return cos(z) void disp( int x ) if( x 0 ) printf(“Positive.n”);else if( x = = 0 ) printf(“Zero.n”);else printf(“Negative.n”); 5 nesting -function.return statement while calling a function, another function is called. fun1( ).fun2( ).fun3( ). fun1( ).fun2( ). nesting-function does not mean nesting-definition. void beijing( );void shanghai ( );void tianjin( );main( ) printf(“I m in main.n”); beijing( );printf(“Im finally back in main( ).n”); void beijing( ) printf(“I m in beijing.n”); shanghai( );printf(“Here Im back in beijing.n”); void shanghai( ) printf(“Now Im in shanghai.n”); tianjin( ); printf(“Now I m back in shanghai.n”); void tianjin( ) printf( “I m in tianjin now.n”); main( ) call func A;func A call func B;func B return statementLocal variables and external variables externalLocalinnerFunctionBlockOut of int x,y;main( ) int a,b,c;fun(a,b);.int fun( int m)shareLocal variables and external variables 1 . local variables Declared within a function or a block . Can only be used in the function or the block which define them. They are created a new each time the function is called, and destroyed on return from the function or outside the block. They are not visible for other functions . Local variables in different functions can share the same name The formal arguments are treated like local variablesfloat f(int a) intb,c; a,b,c are local variablesmain( ) int x=10; int x=20; printf(“%d “, x); printf(“%d“,x ); Local variables and external variables main( ) int x=1;void f1( ),f2( );f1( );f2(x);printf(“%cn”,x );void f1( ) int x=3;printf(“%d”,x );void f2( int x) printf(“%d ”, +x);2 . External variables Defined outside any function Their value is retained and is available to any other function which accesses them. Can be initialized only once Can share the same name with local variable, but the latter is useful within the naming function. should be declared if use it before its definition.Local variables and external variables extern int a;main( ) int I; for(I=1;I=5;I+) +a; printf( ”%dn”,a); s ( ) ; int a=10;s ( ) int a=100;+a; printf(“%dn”, a); Declare an external variableDefine and initialize itDefine and initialize a local variableint x=500;main( ) int x=300; f( ); ff( ); printf(“x=%dn”, x); f ( ) x+=100; printf(“x=%dn”,x); ff ( ) int x=10; printf(“x=%dn”,x); Local variables and external variables int k=1;main( ) int i=4;fun( i );printf(“%d, %d”, i ,k); fun( int m) m+=k; k+=m; char k=B;printf(“%d “, k-A);printf(“%d, %d “, m, k);type name ( arguments) statements 1 definition 2 return:obtain the result by calling function3 declaration:type name( )4 formal arguments and actual arguments6 function nesting call 5 function call: name(actual arugments)C functions may be used recursively; a function may call itself either directly or indirectly.recursion: a function can be describe as a combination of the independent variable and itself .RecursionFunctions that call themselves Can only solve a base caseDivide a problem up intoWhat it can doWhat it cannot doWhat it cannot do resembles original problemThe function launches a new copy of itself (recursion step) to solve what it cannot doEventually base case gets solvedGets plugged in, works its way up and solves whole problemRecursive functions Recursionfactorials 5! = 5 * 4 * 3 * 2 * 1Notice that5! = 5 * 4!4! = 4 * 3! .Can compute factorials recursively Solve base case (1! = 0! = 1) then plug in2! = 2 * 1! = 2 * 1 = 2;3! = 3 * 2! = 3 * 2 = 6;RecursionRecursionExecute processfacto(int n) if (n=0)return(1);else return(n*facto(n-1);facto(int n)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论