1 / 17

This presentation includes custom animations.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode.

brandi
Télécharger la présentation

This presentation includes custom animations.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

  2. User-Defined Functions This lesson discusses the purpose and syntax of user-defined functions in ANSI C: Vocabulary: body calling function header function call library function prototype user-defined function Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  3. A function is … A named group of statements that can be called and evaluated and can return a value to the calling statement. (www.serc.iisc.ernet.in/ComputingFacilities/systems/cluster/vac-7.0/html/glossary/czgf.htm) A named group of statements in a program that perform a task when called on. (www.metromemetics.com/thelexicon/f.asp) A subprogram which returns a value of a specified type which is invoked as part of an expression. (www.adaic.org/docs/craft/html/glossary.htm)

  4. Functions are not a new concept. You have been using functions in every program in this course. A library (or built-in) function is one that is included as part of the language. In C, library functions are defined in the header files (stdio.h, math.h, ctype.h, etc). Some library functions that you have used are: printf(), scanf(), getchar(). A programmer can create new customized functions. A new function that is not part of the library and is written by the programmer, it is called a user-defined function (UDF). One user-defined function that you have written is main() User-defined functions other than main() are sometimes called subfunctions.

  5. User Defined Functions in ANSI C • A user defined function is a collection of code written by the programmer that can be called as a unit from within another function in the program. • Functions serve several purposes: • Eliminate repetitious code • Improve modularization which • Eases understanding for humans • Simplifies maintenance • Increases portability

  6. Consider how a user-defined function can be used to solve the following problem. Final grades are based on the average of 2 midterms and a final. The midterms are each worth 25% of the final grade and the final exam is worth 50%. Example: Test 1 84/100 0.25 * .84 = 0.21 Test 2 80/100 0.25 * .80 = 0.20 Final 90/100 0.50 * .90 = 0.45 = 0.86

  7. Example: Click through to become familiar with the following flowchart and code snippet. int main() { double Test1Score, Test1Possible; double Test2Score, Test2Possible; double Test3Score, Test3Possible; printf("Enter Score on Test 1: "); scanf("\n%lf", &Test1Score); printf("Enter points possible on Test 1: "); scanf("\n%lf", &Test1Possible); Test1Percent = Test1Score / Test1Possible; printf("Enter Score on Test 2: "); scanf("\n%lf", &Test2Score); printf("Enter points possible on Test 2: "); scanf("\n%lf", &Test2Possible); Test2Percent = Test2Score / Test2Possible; printf("Enter Score on Final Exam: "); scanf("\n%lf", &FinalScore); printf("Enter points possible on Final Exam: "); scanf("\n%lf", &FinalPossible); FinalPercent = FinalScore / FinalPossible; OverallScore = 0.25 * Test1Percent + 0.25 * Test2Percent + 0.50 * FinalPercent; printf("\nThe overall score is %5.2f", OverallScore; return 0; {

  8. There are 3 steps to coding and using a function: • Definition • Prototype • Call

  9. The definition of a function is that part of the source code where the programmer lists the instructions that will be executed when the function is invoked. The definition comes AFTER main(); int CalcArea( int Width, int Length) { int Area; Area = Width * Length; return Area; } DEFINITION

  10. The prototype of a function is an outline of the function provided to the compiler. The prototype comes BEFORE main() PROTOTYPE int CalcArea( int Width, int Length) ; int CalcArea( int Width, int Length) { int Area; Area = Width * Length; return Area; } DEFINITION

  11. The function call is a line of code in one function that invokes another (or the same) function. PROTOTYPE int CalcArea( int Width, int Length) ; int main() { int Side1; int Side2; int SquareFootage; scanf("\n%d %d",&Side1, &Side2); SquareFootage = CalcArea(Side1, Side2); printf("\nTotal Square Feet is: %d", SquareFootage); return 0; } CALL int CalcArea( int Width, int Length) { int Area; Area = Width * Length; return Area; } DEFINITION

  12. What flowcharting symbol is used to represent a function call? How many values can a function return? 0 or 1 What flowcharting symbol is used to represent a returned value? What flowcharting symbol is used when there is more text than can fit into a regular shape? After all steps in a function are completed, what is the next instruction executed? The first instruction following the function call.

More Related