DOC PREVIEW
UW CSE 142 - Functions I

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 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 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

G1-1G1-110/9/00CSE 142Computer Programming IFunctions I© 2000 UW CSEG1-2OverviewConcepts this lectureFunctionsFunction control flowTwo meanings of voidPre-written functionsG1-3Chapter 3Read All!3.1: Reusing program parts3.2: Built-in math functions3.3: Top-Down Design3.4: Functions with no parameters3.5: Functions with parametersG1-4Control Flow: Review“Control flow” is the order in which statements are executedWe’ve discussed two forms of control flow so far: sequential and conditional (in more than one flavor)G1-5Another Form of Control Flow“Functions”(or“procedures”or “subroutines”)allow you to “visit” a chunk of code and then come backThe function maybe elsewhere in your own program, or may be code in another file altogetherG1-6Why Use Functions? Here’s one example:Suppose we are writing a program that displays many messages on the screen, and…We'd like to display two rows of asterisks (‘*’s) to separate sections of output:****************************************G1-2G1-7Moving Toward a SolutionThe result we want is this:****************************************And the basic code needed is this:printf("********************\n");printf("********************\n");G1-8#include <stdio.h>int main(void){/* produce some output */.../* print banner lines */printf("********************\n");printf("********************\n");/* produce more output */.../* print banner lines */printf("********************\n");printf("********************\n");A Full Solution/* produce even more output */.../* print banner lines */printf("********************\n");printf("********************\n");/* produce final output */...return 0 ;}G1-9Anything Wrong With This?It’s correct C codeIt fulfills the problem specification, i.e., gives the desired resultG1-10Anything Wrong With This?It’s correct C codeIt fulfills the problem specification, i.e., gives the desired resultWhat’s “wrong” has to do with other issues such ashow hard it would be change the program in the futureHow much work is it to write the same statements over and over...G1-11What if...Later on the client wants us to change...The number of rows of asterisksThe number of asterisks per rowUse hyphens instead of asterisksPrint the date and time with each separator...How much work is involved?G1-12… have to edit every “copy” of the code in the program.… it’s easy to overlook some copies.… it can be hard to find them all (because they might not be written identically).… it can be hard to find them all because code written identically may not serve the same logical purpose.If We Want to Change AnythingG1-3G1-13Identify a “sub-problem” that has to be solved in your programSolve that sub-problem and write the code for it only onceGive that code a name: that makes it a functionWhenever you see that same sub-problem again, use the function name to say “go to that code now to take care of this problem, and don’t come back until you’re done”One (Big) Idea Behind FunctionsG1-14PrintBannerLines FunctionFor our print banner program, that strategy means this:Take the repeated lines of codeprintf("********************\n");printf("********************\n");and wrap them up as a function, which we can call printBannerLinesG1-15#include <stdio.h>int main(void){/* produce some output */PrintBannerLines();/* produce more output */PrintBannerLines();/* produce more output */PrintBannerLines();/* produce final output */return 0 ;}printf("********************\n");printf("********************\n");The code named PrintBannerLinesG1-16Discussion QuestionIn the new version of the program:what do we have to do now if we want to change the banner? How many places in the program have to be changed?G1-17The Big Picture, So FarYou’ve now some colossal concepts:FunctionsFunction control flowThe motivation for functionsComing right up...Syntax for defining a functionBuilt-in C functionsG1-18Syntax for Defining the PrintBannerLines Function/* write separator line on output */void PrintBannerLines (void){printf("***************\n");printf("***************\n");}This is a typical pattern for a function declarationG1-4G1-19Two Key Features1. The name of the function and 2. the function body: code that is to be executed when the function is called./* write separator line on output*/void PrintBannerLines (void){printf("***************\n");printf("***************\n");}function body (statements to be executed). A function can have ANY number of ANY kind of statements.function nameheading commentG1-20/* write separator line on output*/void PrintBannerLines (void){printf("***************\n");printf("***************\n");}Further details: voidThe keyword void has two different roles in this function definition.indicates that the function has no parameters.indicates that the function doesnot return a value.G1-21Oops – Two New Concepts1. Return values: we will postpone for now2. Parameters: We will postpone this, too! Both concepts are very important in general, but not for this particular example/* write separator line on output*/void PrintBannerLines (void)...G1-22Using PrintBannerLines#include <stdio.h>void PrintBannerLines (void){printf("***************\n");printf("***************\n");} int main (void){/* produce some output */…PrintBannerLines( );...return 0;}Empty ( ) is required when a parameter-less (void) function is called.The definition of the function must precede all calls to it in the file.G1-23Some C FunctionsWe have already seen and used several functions:int main (void) { return 0; }printf ("control", list);scanf ("control", &list);Function definition for main( )Calls to the functionsprintf( ) and scanf( )G1-24Pre-written functionsPre-written functions are commonly packaged in "libraries”Every standard C compiler comes with a set of standard librariesRemember #include <stdio.h> ?•Tells the compiler you intend to use the “standard I/O library” functions•printf and scanf are in the standard I/O library•So are lots of other I/O related functionsThere are (many) other useful functions in other librariesG1-5G1-25Next TimeWe’ll continue our discussion about functions. We will examine how values are passed to functions, and how values come


View Full Document

UW CSE 142 - Functions I

Download Functions I
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 Functions I 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 Functions I 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?