REV 00 Chapter 3 Functions DDC 1123 PENGATURCARAAN

  • Slides: 12
Download presentation
REV 00 Chapter 3 Functions DDC 1123 PENGATURCARAAN I (C) 1

REV 00 Chapter 3 Functions DDC 1123 PENGATURCARAAN I (C) 1

REV 00 3. 1 Designing Structured Programs �Structured programming is a problem solving strategy

REV 00 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 top-down design. DDC 1123 PENGATURCARAAN I (C) 2

REV 00 3. 1 Designing Structured Programs �Involves repeatedly decomposing a problem into smaller

REV 00 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. DDC 1123 PENGATURCARAAN I (C) 3

REV 00 3. 2 Functions �A C program is made up of one or

REV 00 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. DDC 1123 PENGATURCARAAN I (C) 4

REV 00 3. 2 Functions �Standard library functions �Collection of functions for performing useful

REV 00 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 DDC 1123 PENGATURCARAAN I (C) 5

REV 00 3. 3 User Defined Functions n Declarations: return. Type function. Name (

REV 00 3. 3 User Defined Functions n Declarations: return. Type function. Name ( parametet. List ) { declarations statements } void print. Greeting ( void ) { printf ("Hello World!"); } Header Body Example DDC 1123 PENGATURCARAAN I (C) 6

REV 00 3. 3 User Defined Functions n Declarations: return. Type function. Name (

REV 00 3. 3 User Defined Functions n Declarations: return. Type function. Name ( parametet. List ) n Function prototype Functions must be declared before they are called DDC 1123 PENGATURCARAAN I (C) 7

REV 00 3. 4 Standard Library Functions �Collection of functions for performing useful operation

REV 00 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. DDC 1123 PENGATURCARAAN I (C) 8

REV 00 3. 4 Standard Library Functions � Informs the compiler that there will

REV 00 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 print. Message (void) ; � Needed because the function call is made before the definition -- the compiler uses it to see if the call is made properly DDC 1123 PENGATURCARAAN I (C) 9

REV 00 3. 4 Standard Library Functions �Passes program control to the function �Must

REV 00 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 print. Message (void) ; int main ( ) same no arguments { print. Message ( ) ; return 0 ; } DDC 1123 PENGATURCARAAN I (C) 10

REV 00 3. 5 Scope �File scope ◦ Identifier defined outside function, known in

REV 00 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. ) DDC 1123 PENGATURCARAAN I (C) 11

REV 00 3. 5 Scope �Block scope ◦ Identifier declared inside a block �Block

REV 00 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 DDC 1123 PENGATURCARAAN I (C) 12