Presentation is loading. Please wait.

Presentation is loading. Please wait.

Character (String) Data

Similar presentations


Presentation on theme: "Character (String) Data"— Presentation transcript:

1 Character (String) Data
Character data in an programming language is always a little tricky. CHARACTER name (for single character variables) CHARACTER name(n) for strings of characters The tricky part always comes down to: CHARACTER name(100) = ‘Duncan Buell’ The first 12 characters of this string are obviously ‘Duncan Buell’, but what gets stored in the rest of the string? CSCE 206

2 Character (String) Data
Character strings in scientific computing are usually labels on other data. There are functions that handle character strings. There is a collating sequence or sort order for character strings. Usually character data will be sorted lexicographically (in dictionary order), with the strings left justified. (We may or may not get to the issue of the collating sequence.) CSCE 206

3 The End CSCE 206

4 Subprograms The power of a computer comes from conditional execution (IF-THEN-ELSE) and from iteration (DO loops). Programs would be much less useful if done as straight line code. Subprograms in Fortran are either subroutines or functions. Functions work like mathematical functions—arguments are passed in to the function, and a function value is returned. Subroutines allow for returning more than one value. CSCE 206

5 Builtin Functions See pages 324-325 for some builtin functions.
CSCE 206

6 Functions DO n = 1, 10 PRINT *,a*n**2 + b*n + c END DO
can be replaced by f = f_of_n(a,b,c,n) PRINT *,a,b,c,n,f then later on INTEGER FUNCTION f_of_n(a,b,c,n) INTEGER :: a,b,c,n f_of_n = a*n**2 + b*n + c RETURN END CSCE 206

7 Functions -- Rules Variables in the calling program are completely independent of variables in the subprogram, so you can have an x in each and they are different. The region of code over which a variable is defined is called its scope. Variables must be declared in the subprogram. If the value of variable that is an argument to a subprogram is changed inside the subprogram, then that change is propagated to the calling program as well. Unless the INTENT(IN) spec is used. CSCE 206

8 The End CSCE 206

9 Functions—Internal, External, Modules
Four ways to do functions—this is too many, IMO I cover this only to explain what you’ll find in the book. Essentially, the only differences are in the scope of the function names and the variables. And everything I say about functions is also true for subroutines CSCE 206

10 First: A Word about Compilation
When the compiler has finished, virtually all of the symbols in your program have disappeared This sort of includes the function names themselves, which is the point of this whole section of the lecture. If your calling program wants to call a function, there has to be a way for the function’s symbol (i.e., name) to be recognizable by the compiler. CSCE 206

11 ONE: EXTERNALLY Defined Functions
PROGRAM version_one IMPLICIT none INTEGER my_function f = my_function(arguments) END PROGRAM version_one INTEGER FUNCTION my_function(arguments) my_function = whatever RETURN END my_function The function code lies OUTSIDE the calling program. CSCE 206

12 TWO: INTERNALLY Defined Functions
PROGRAM version_one IMPLICIT none ! INTEGER my_function (NO LONGER NECESSARY) f = my_function(arguments) STOP CONTAINS INTEGER FUNCTION my_function(arguments) my_function = whatever RETURN END my_function END PROGRAM version_one The function code lies INSIDE the calling program. CSCE 206

13 Functions—Internal, External, Modules
External: The function name must be declared and typed in the calling program, because the function “isn’t visible” outside the calling program. Internal: The function name must NOT be declared, because the INTEGER FUNCTION (or similar) declaration does the declaration. But you need the CONTAINS statement—in essence, this tells the compiler that what follows are functions/subroutines and not CSCE 206

14 Functions—Internal, External, Modules
External: The functions are compiled as independent units. All the variables have local scope. This has advantages we won’t go into now. Internal: Everything about the functions can be hidden, which facilitates the current fashionable mode of object-oriented programming. Variables in the main program have global scope unless they are overridden in subprograms. Not everyone is a fan of OO. I personally find the internal method to be harder to keep straight in my head, but I’ll deny this if you report me to others. CSCE 206

15 THREE: Functions As Modules
It is possible to define a function in one file and its calling program in another file, and then to compile the two files in a multistep process. In this case, there needs to be a way to link the symbols together, because the compilation processes are independent and wash away all the symbols. This is the way a lot of packages are written. (This makes your own functions look like the builtin functions.) CSCE 206

16 Functions—Internal, External, Modules
External: The functions are compiled as independent units. This has advantages we won’t go into now. Internal: Everything about the functions can be hidden, which facilitates the current fashionable mode of object-oriented programming. Not everyone is a fan of OO. But don’t say I told you so. CSCE 206

17 Functions—Bottom Line
With external functions, or with separately compiled files, the ONLY symbols that still exist after the compile step are the names of the subprograms. All other symbols are local to the functions and have disappeared by the time the compilation finishes. This isn’t all that difficult to deal with. If you forget to declare something, then the compiler messages will indicate that by saying that the variables aren’t defined. CSCE 206

18 The End CSCE 206

19 Subroutines Functions take arguments and return a single value, just as does a mathematical function. Sometimes you want to use a subprogram because you’re going to use the same piece of code over and over again, and you want to encapsulate that code (and be able to guarantee that you can use variable names that won’t conflict with names used elsewhere in the program). CSCE 206

20 Subroutines SUBROUTINE my_sub_name(arguments) instead of
REAL FUNCTION my_func_name(arguments) Called with CALL my_sub_name(arguments) instead of by invoking with variable_name = my_func_name(arguments) CSCE 206

21 Subroutines Since subroutine names are not symbols with data types, they do NOT need to be declared. And the name is NOT given a value inside the subroutine before executing the RETURN. Otherwise, all the same rules apply, with regard to local or global scoping, internal or external, etc…. CSCE 206

22 The End CSCE 206


Download ppt "Character (String) Data"

Similar presentations


Ads by Google