Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Dynamic-link library wikipedia , lookup
Algorithm characterizations wikipedia , lookup
Functional programming wikipedia , lookup
Recursion (computer science) wikipedia , lookup
Name mangling wikipedia , lookup
Structured programming wikipedia , lookup
Operational transformation wikipedia , lookup
Dirac delta function wikipedia , lookup
APL syntax and symbols wikipedia , lookup
REV00 Chapter 3 Functions DDC1123 PENGATURCARAAN I (C) 1 REV00 3.1 Designing Structured Programs Structured programming is a problem solving strategy and a programming methodology that includes the following guidelines: ◦ The program uses only the sequence, selection, and repetition control structures. ◦ The flow of control in the program should be as simple as possible. ◦ The construction of a program embodies topdown design. DDC1123 PENGATURCARAAN I (C) 2 REV00 3.1 Designing Structured Programs Involves repeatedly decomposing a problem into smaller problems Eventually leads to a collection of small problems or tasks each of which can be easily coded The function construct in C is used to write code for these small, simple problems. DDC1123 PENGATURCARAAN I (C) 3 REV00 3.2 Functions A C program is made up of one or more functions, one of which is main( ). Execution always begins with main( ), no matter where it is placed in the program. By convention, main( ) is located before all other functions. When program control encounters a function name, the function is called (invoked). ◦ Program control passes to the function. ◦ The function is executed. ◦ Control is passed back to the calling function. DDC1123 PENGATURCARAAN I (C) 4 REV00 3.2 Functions Standard library functions Collection of functions for performing useful operation ( mostly written by computer vendors ) Functions such as printf and scanf are standard library functions DDC1123 PENGATURCARAAN I (C) 5 REV00 3.3 User Defined Functions Declarations: returnType functionName ( parametetList ) { declarations statements } void printGreeting ( void ) { printf ("Hello World!"); } Header Body Example DDC1123 PENGATURCARAAN I (C) 6 REV00 3.3 User Defined Functions Declarations: returnType functionName ( parametetList ) Function prototype Functions must be declared before they are called DDC1123 PENGATURCARAAN I (C) 7 REV00 3.4 Standard Library Functions Collection of functions for performing useful operation ( mostly written by computer vendors ). Functions such as printf, scanf,and getchar and are standard library functions. Programmers can write their own functions. C function names follow the same naming rules as C variables. DDC1123 PENGATURCARAAN I (C) 8 REV00 3.4 Standard Library Functions Informs the compiler that there will be a function defined later that: returns this type has this name takes these arguments void printMessage (void) ; Needed because the function call is made before the definition -- the compiler uses it to see if the call is made properly DDC1123 PENGATURCARAAN I (C) 9 REV00 3.4 Standard Library Functions Passes program control to the function Must match the prototype in name, number of arguments, and types of arguments void printMessage (void) ; int main ( ) same name { printMessage ( ) ; return 0 ; } no arguments DDC1123 PENGATURCARAAN I (C) 10 REV00 3.5 Scope File scope ◦ Identifier defined outside function, known in all functions ◦ Used for global variables, function definitions, function prototypes Function scope ◦ Can only be referenced inside a function body ◦ Used only for labels (start:, case: , etc.) DDC1123 PENGATURCARAAN I (C) 11 REV00 3.5 Scope Block scope ◦ Identifier declared inside a block Block scope begins at declaration, ends at right brace ◦ Used for variables, function parameters (local variables of function) ◦ Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block Function prototype scope ◦ Used for identifiers in parameter list DDC1123 PENGATURCARAAN I (C) 12