1 / 22

Character (String) Data

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’

Download Presentation

Character (String) Data

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

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

  3. The End

  4. Subprograms The power of a computer comes from conditionalexecution (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.

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

  6. Functions DO n = 1, 10 PRINT *,a*n**2 + b*n + c END DO can be replaced by DO n = 1, 10 f = f_of_n(a,b,c,n) PRINT *,a,b,c,n,f END DO 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

  7. Functions -- Rules Variables in the callingprogram 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.

  8. The End

  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

  10. First: A Word about Compilation When the compiler has finished, virtuallyall of the symbols in your program have disappeared This sort of includes the functionnamesthemselves, 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.

  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.

  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.

  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

  14. Functions—Internal, External, Modules External: The functions are compiled as independent units. All the variables have localscope. 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.

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

  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.

  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.

  18. The End

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

  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)

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

  22. The End

More Related