{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

P.Lecture - 2.7.08

# P.Lecture - 2.7.08 - Announcements Assignment 2 due today...

This preview shows pages 1–10. Sign up to view the full content.

Announcements • Assignment 2 due today! •Exam 1 next Thursday –50 minute exam (9:30 – 10:20) • Topics for today nctions Functions –Scope of variables 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Functions in a Nutshell •A named collection of statements •S o m e w h a t like mathematical functions: y = f(x) –A r g um e n t s are not required –D o e s not need to return a value (similar to a procedure in other languages) but can • Functions are like “mini programs” called by name –d e c l a r a t i o n s body • Previously written standard functions are in the library for your use • You can write your own functions too 2
A Function as a Module in Subprocess/ out lues sent in Resultant values returned, subprogram values sent in from function calling statement or function actions have me other function via arguments (optional) some other effect As in, for example: double y = sqrt (x); double y = square_it (x); tchar ( ); 3 getchar ( );

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
C Standard Library Functions •See appendix D • In various header files (.h) – stdlib – stdio –math – time – ctype ring – string –assert few others 4 a few others
Top Down Functional Programming ain main calls sub1 sub2 sub3 sub4 lls lls subsub1 subsub2 subsub3 subsub4 calls calls sss1 sss4 calls 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
How does this work? • Partitioning the problem –Le t s us divide a big problem into small problems –No one part is highly complex –Mu s t balance having too many and too few functions •Too few leads to complexity of the size of each •Too many leads to complexity of the number of interconnects any ways to partition a problem Many ways to partition a problem –one of the fundamentals of the craft of design nctions are key to Functions are key to – Reducing complexity – Increasing clarity 6 – Increasing reusability
Function Mechanics Functions allow us to modularize and encapsulate the processes of our programs. This is key to handling complexity.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Defining a Function • Function operates on data passed through the parameter list • Function either returns a value computed or causes some action to occur • Called by name from some other function (e.g., main) • Function definition consists of a header and a body inside { } nction Header Function Header –H a s a return data type (such as int, double, char or void) a s a function name (such as squareIt) as a list of formal parameters (such as: double amount) Has a list of formal parameters (such as: double amount) •a parameter is a local variable declaration for an argument value to be sent into the function from the calling statement nction body Function body { // the set of declarations and statements that defines the implementation } nction prototype Function prototype –I s a function header that must appear before its use (compiler requirement)
Function Definition (cont) ntax of a function definition: Syntax of a function definition: return type function name ( parameter list ) { declarations statements } Parameter list ch

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 36

P.Lecture - 2.7.08 - Announcements Assignment 2 due today...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online