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); }