Presentation is loading. Please wait.

Presentation is loading. Please wait.

Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3

Similar presentations


Presentation on theme: "Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3"— Presentation transcript:

1 Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Functions Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3

2 What is a function? Simplistically a function is a procedure which returns a value. You have been using functions for some time now, for example: trunc round sqrt etc.

3 Function Declaration Form
Functions take the form: FUNCTION [name] (param list) : [type returned by the function]; CONST {declarations} VAR BEGIN { body } [name] := [ some value ]; END;

4 Function Declaration Example
FUNCTION CubeIt( x: integer) : integer; Begin CubeIt := x * x * x; End;

5 Differences between procedures and functions
A function is given a type in the function declaration heading. Somewhere within the body of the function declaration, the function name must appear on the left-hand side of an assignment statement.

6 Arguments versus Parameters
Arguments and parameters are more or less the same thing. The only distinction is that arguments are commonly associated with functions and parameters are associated with procedures.

7 Challenge (not graded)
Write a program that breaks a number into the sum of powers of 2 + (some small extra) if needed. e.g. 800 = e.g. 803 = Write a program that has a function to calculate x^n. =) Can you use the first program to “speed up” things in the second program? Consider: x^4 = (x^2)^2 x^16 = (x^4)^2 x^32 = (x^16)^2 x^512 = (x^256)^2

8 Function Rule ! A function should NEVER change the value of anything outside the function! A well written function will never change the value of anything outside the function.

9 Function guidelines Functions should only return ONE value.
Don’t use variable parameters to return values when you are creating functions. Functions should not have any variable parameters. i.e. Whenever possible send value parameters to functions – this decreases the chances of accidentally changing something outside the function. Functions should not input or output anything.

10 Side effect If a function
changes the value of a global variable (or a variable outside its own scope) sets the value of a variable parameter or causes a read or write statement to be executed. that extra feature is referred to as a side effect. Avoid side effects !

11 Locals Functions may have local constants, local variables, local procedures, local functions. For the most part I would discourage using local procedures and local functions, but know that they can exist and there may be scenarios where they are useful.

12 Common Error Within a given function called [name], the function name cannot appear on the right hand side of an assignment operator. For example: Function Graham( x: real) : real; VAR y : real; BEGIN y := Graham * 4.5; { This line is an ERROR } END; [name] however can appear multiple times on the left hand side of an assignment operator. In fact it MUST so appear at least once.

13 READ SECTION 5.3 !!! You should read section 5.3

14 Suggested Exercises (not graded)
page 184: 13, 14, 15, 16, 17 page 193: 22 programs: page 195: 23, 24 page 196: 27, 28, 30 page 197: 35, 37

15 End Functions Thus ends chapter 5.


Download ppt "Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3"

Similar presentations


Ads by Google