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)