DOC PREVIEW
UW CSE 142 - Program Files

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

University of Washington Computer Programming IStructuring ProgramsOrder in the ProgramPowerPoint PresentationFunction PrototypesSolution: Function PrototypesExample Function PrototypesUsing PrototypesLibrary Functions#include <stdio.h>Compilers, Linkers, etc.Putting it All TogetherLogical Order vs. Control FlowSummaryG3-1University of WashingtonComputer Programming IStructuring Program Files© 2000 UW CSEG3-2Structuring ProgramsThe function is the basic unit of a C programPrograms often use many functionsSome are defined within the programSome are in librariesOrganizing and ordering the functions and other parts within the .c file is importantG3-3Order in the ProgramGeneral principle: identifiers (names of things) must be declared before they are used.Variables:place them first within each function#define constants:placed at the top of the .c fileWhat about functions?G3-4Order for Functions in the .c FileFunction names are identifiers, so… they too must be declared before they are used:#include <stdio.h>void fun2 (void) { ... }void fun1 (void) { ...; fun2(); ... }int main (void) { ...; fun1(); ... return 0; }fun1 calls fun2, so fun2 is defined before fun1, etc.G3-5Function PrototypesInsisting that all the code of each function precede all calls to that function is sometimes:Impossible: function A calls B, and B calls AInconvenient: printf() is a function, but we don’t want its code in our programBut the ordering rule requires that the function names be declared before they can be used (in a call).Is there any solution?G3-6Solution: Function PrototypesFunction prototypes allow us to define the name, so that it can be used, without giving the code for the function. The prototype gives the function name, return type, and the types of all the parameters but no code. In place of the { } code block, there is a semicolon.G3-7Example Function Prototypesvoid Useless(void);void PrintInteger(int value);double CalculateTax (double amount, double rate);G3-8Using PrototypesWrite prototypes for your functions near the top of the programCan use the function anywhere thereafterFully define the function later, wherever convenientHighly recommended to aid program organizationG3-9Library FunctionsWhat about library functions, like printf? You must also tell the compiler that you are going to use the library which contains printfThis is the purpose of the #include directiveThe linker knows where the libraries areG3-10#include <stdio.h>The “#include” means “go get the file stdio.h and insert what’s in it right here (as if it had been typed here)”stdio.h contains function prototypes for scanf and printf and the other functions in the standard I/O library The actual code for them is NOT there, just prototypes. The (result of compiling) the code is in a library that is combined with your code by the linkerG3-11Compilers, Linkers, etc.library (ANSI)header(stdio.h)executableprogramcompilerlinkersourcecodeobject code.c file011010001101G3-12Putting it All Together#include directives…#define constants…Function prototypes…Full function definitions…G3-13Logical Order vs. Control FlowWith prototypes, the functions can be placed in any physical orderOrder within the source file has no influence on control flowPrograms always start at the function mainSo there should always be a mainNo function is executed until it is called by some other functionOnly exception: mainG3-14SummaryOrganizing the parts of a .c file is importantGeneral principle: identifiers must be declared before they are usedFor functions, a prototype can be declared near the beginning of the programFunction can be defined in detail laterFor libraries, mention the library name in a #include directiveSource order and control flow are different


View Full Document

UW CSE 142 - Program Files

Download Program Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Program Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Program Files 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?