• No results found

Same old idea: breaking problem into subproblems. Names: subprogram, function, sub(routine), mod(ule), procedure

N/A
N/A
Protected

Academic year: 2021

Share "Same old idea: breaking problem into subproblems. Names: subprogram, function, sub(routine), mod(ule), procedure"

Copied!
18
0
0

Loading.... (view fulltext now)

Full text

(1)

Ch.6 Functions

Same old idea: breaking problem into subproblems.

Names: subprogram, function, sub(routine), mod(ule), procedure

6.1 Intrinsic functions → please review sec. 2.3

Text example MOD: p.253

6.2 Inline functions (text calls them statement functions) Example not from text:

(2)

Example in text: calculating area of triangle: p.255

Inline functions are limited to one assignment.

What if we need more complex decisions, e.g. what if we want to make sure the result in the rainfall formula is not negative?

6.5 User-defined Functions

Example not from text:

(3)

Things to remember:

The structure of a function closely resembles that of the main program. Differences:

Functions have a type. This type must also be declared in the calling program. If we use a function which has not been declared, the compiler will try to use the same implicit typing used for variables, sometimes getting it wrong.

The return value should be stored in a variable with the same name as the function.

Functions are exited by the return statement. Syntax of a FORTRAN 77 function:

type function name (list-of-variables)

declarations

statements

return

end

The function is called by simply using the function name and listing the parameters in parentheses. When the program runs, the name is replaced by the value returned.

Read for next time: 6.4 Modular Programming

(4)

---Week 6, lect.3

QUIZ: Write a short program that calculates the area of a trapezoid

A

TRAP

= h (BASE + base)/2

using an

inline function. The 3 parameters needed are read from the KBD, and the area is printed on the screen.

(5)

Common mistake: Not declaring the function type in the calling program The type will be given by default by the first letter of the function name! (Even if the function itself has been correctly declared of a different type!)

(6)

Standard F77 doesn't permit recursion (it was introduced in F90). Many F77 compilers allow recursion (but Force does not allow it!).

(7)

It is possbile to pass an array as an argument to a function

p.269 → function that calculates the average of an array of 20 elements

What if we wanted the number of elements to be flexible?

pp.270-271 → function that calculates the median of a sorted array

This time the number of elements is passed as argument!

p.296: Function found() searches key among the first count elements of an array x.

Dynamic array? Not quite.

(8)

Real-life issue: how to “merge” functions into our own programs. Write small “driver” programs.

Individual work for Monday:

Read and understand the driver program TEST on p.271.

(9)

Week 6, lect.3

QUIZ: This code for function AVE() is presented on p.269 of our text:

REAL FUNCTION AVE(X) INTEGER I

REAL X(20), SUM SUM = 0.0

DO 10 I=1,20

SUM = SUM + X(I) 10 CONTINUE

AVE = SUM/20.0 RETURN

END

Rewrite the function so it takes as a second argument the number N of array elements.

The following code is presented for the driver:

PROGRAM DRIVER INTEGER ID, I

REAL SCORES(20), HWAVE, AVE READ *, (SCORES(I), I=1,20) HWAVE = AVE(SCORES)

PRINT *, HWAVE END

(10)

Call-by-reference (not in text!)

F77 uses the so-called call-by-reference mechanism for passing values between functions.

This means that instead of just passing the values of the function arguments (a.k.a. call-by-value), what is being passed is memory addresses of the arguments.

Since the function has access to the original variables, it follows that any changes made to these variables inside the function will be be “seen” outside, in the calling program.

This argument-passing mechanism is briefly alluded to at point 10. on p.264 of the text:

“[…] return the new values to the main program” means call-by-reference. Be careful about this when writing Fortran code, because it is easy to introduce undesired side effects. For example, sometimes it is tempting to use an input

parameter in a function as a local variable and change its value. Since the new value will then propagate back to the calling program with an unexpected value, you

should never do this unless the change is part of the purpose of the function (e.g. in the iswap function above).

(11)

Passing 2-D arrays to functions (Example 6-6)

When the size of the array passed to the function is not equal to the size of the original array (from the main program), the function will “get

confused” → see 5x7 example in text.

Solution: pass the original dimensions, as well as the ones actually used in the function → p.275.

Individual work for next time:

Read and understand Example 6-6 (pp.271-275)

(12)

Week 7, lect.2

QUIZ:

(13)

6-7 Roots of polynomials

Horner’s method for evaluating polynomials

Incremental (a.k.a. linear) search

Although either search method can be applied by itself, the arrangement above is common: we use linear search to find an interval where the root lies, then narrow it down with logarithmic search.

(14)

Bisection (a.k.a. logarithmic) search

… and now all we need to do is see why the loop exited:

(15)

Week 7, lect.3

6-8 Functions for search algorithms

The ideas are similar to the ones from root search. Differences:

The values don’t need to be generated, they are already stored in array

We’re not looking necessarily for zero, but for an arbitrary key If the array is not sorted:

(16)

Is the algorithm above more similar to incremental search or bisection search?

(17)

If the array is sorted:

Compare side-by-side the pseudocodes on p.296 and 299. What is the difference?

The new algorithm is called sequential search with early termination

What is the complexity of SSWET? (Best, Worst, Average)

Even better idea for sorted arrays: binary search

(18)

found (x, count, key): IF count ≤ 0 then

found false

ELSE if x(1) > key OR x(count) < key THEN found false ELSE done false found false first 1 last count WHILE not done DO

middle (first+last)/2 IF x(middle) = key then

found true done true IF not done THEN

IF first ≥ last THEN done true

ELSE IF x(middle) > key THEN last middle–1

ELSE

first middle+1 RETURN

Explain middle = (first + last)/2

Is it correct to use x(middle)?

Integer division makes things easier! (How would we do it without int.div.?)

Pencil-and-paper example on p.301

Homework for Ch.6:

Due before midterm! (Midterm is 1 week from now, during lab. Class of that day is review.)

End-of-chapter 2, 8

Not from text: initialize an array of 20 REALs with random numbers, and then find the value of its maximum using a function.

References

Related documents

The analysis of the given definitions allows to come to a conclusion that the trust in its classical understanding is the transfer of the property by

The PROMs questionnaire used in the national programme, contains several elements; the EQ-5D measure, which forms the basis for all individual procedure

Further, by showing that v τ is a modular unit over Z we give a new proof of the fact that the singular values of v τ are units at all imaginary quadratic arguments and obtain

Prior to the visual search displays, observers were presented with a colour cue that indicated either the target colour (positive cue), the colour of the to-be-ignored

b In cell B11, write a formula to find Condobolin’s total rainfall for the week.. Use Fill Right to copy the formula into cells C11

○ If BP elevated, think primary aldosteronism, Cushing’s, renal artery stenosis, ○ If BP normal, think hypomagnesemia, severe hypoK, Bartter’s, NaHCO3,

everyone else who were not members of the First or Second Estates. There was an enormous logistical problem with the way in which the estates were divided for example, the First

A consequence of this perspective of woman directors being appointed as part of the symbolic management of the independence of the board is that, when these directors lose