Presentation is loading. Please wait.

Presentation is loading. Please wait.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Similar presentations


Presentation on theme: "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."— 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 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 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

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 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() 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(). User-defined functions other than main() are sometimes called subfunctions. Functions are not a new concept. You have been using functions in every program in this course.

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: 1.Eliminate repetitious code 2.Improve modularization which a)Eases understanding for humans b)Simplifies maintenance c)Increases portability

6 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/1000.25 *.84 = 0.21 Test 2 80/1000.25 *.80 = 0.20 Final 90/1000.50 *.90 = 0.45 = 0.86 Consider how a user-defined function can be used to solve the following problem.

7 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; int main() { double Test1Score, Test1Possible; double Test2Score, Test2Possible; double Test3Score, Test3Possible; return 0; { Example: Click through to become familiar with the following flowchart and code snippet.

8

9

10

11

12

13 There are 3 steps to coding and using a function: Definition Prototype Call

14 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

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

16 The function call is a line of code in one function that invokes another (or the same) function. int CalcArea( int Width, int Length) { int Area; Area = Width * Length; return Area; } int CalcArea( int Width, int Length) ; PROTOTYPE DEFINITION CALL 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; }

17 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.


Download ppt "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."

Similar presentations


Ads by Google