SlideServe
Menu
Préférences
© SlideServe · v2026

Basic Building Blocks

alia avatar
56 diapositives · 789 Views · Sep 08, 2012

Seven Golden Rules. Always plan ahead Develop in stages Modularize Keep it simple Test throughly Document all programs Enjoy your programming. Programs and modules. Main program unitprogram nameuse statements...Specification statements...Executable statements...e

Basic Building Blocks - slide 1
01 / 56Basic Building Blocks
1 / 17

Télécharger la présentation

Basic Building Blocks
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation

    1. Basic Building Blocks

    3. Programs and modules Main program unit program name use statements . . . Specification statements . . . Executable statements . . . end program name

    4. Modules

    5. Modules

    6. Module program unit module name use statements . . . Specification statements . contains (Procedure definitions) subprogram_1 subprogram_2 . . subprogram_n end module name

    7. Procedures

    8. Procedures Divide and rule!

    9. Procedures Procedures - origin “Write your own” (homemade) Intrinsic (built-in, comes with F ) sin(x), cos(x), abs(x), … Written by someone else (libraries) Procedures (subprograms) – form Functions Subroutines

    10. Procedures The main program and any subprogram need never be aware of the internal details of any other program or subprogram!

    11. Procedures

    12. Procedures

    14. Subprograms

    15. Functions function name (d1, d2, …) result(result_name) Specifications part . . Execution part end function name Variables Internal (local) variables Result variable (keyword result) Dummy argument (keyword intent(in))

    16. Functions function cube_root result(root) ! A function to calculate the cube root of ! a positive real number ! Dummy argument declaration real, intent(in) :: x ! Result variable declaration real :: root ! Local variable declaration real :: log_x ! Calculate cube root by using logs log_x = log(x) root = exp(log_x/3.0) end function cube_root

    17. Subroutines subroutine roots (x, square_root, cube_root, fourth_root, & fifth_root) ! Subroutine to calculate various roots of positive real ! Number supplied as the first argument, and return them in ! the second to fifth arguments ! Dummy argument declarations real, intent(in) :: x real, intent(out) :: square_root, cube_root, & fourth_root, fifth_root ! Local variable declaration real :: log_x ! Calculate square root using intrinsic sqrt square_root = sqrt(x) ! Calculate other roots by using logs log_x = log(x) cube_root = exp(log_x/3.0) fourth_root = exp(log_x/4.0) fifth_root = exp(log_x/5.0) end subroutine roots

    18. Subroutines call name (arg1, arg2, …) intent(in), intent(out), intent(inout)

    19. Arguments Actual arguments in the calling program Dummy arguments in the subroutine or function The order and types of the actual arguments must correspond exactly with the order and types of the corresponding dummy arguments

    20. Objects Local variables vs. global variables private vs. public objects

    21. Saving the values of local objects Local entities within a procedure are not accessible from outside that procedure Once an exit has been made, they cease to exist If you want their values to ‘survive’ between calls, use real, save :: list of real variables

    22. Example

    23. Example: Write a subprogram which calculates the cube root of a positive real number

    25. Attributes intent (in): the dummy argument only provides information to the procedure and is not allowed to change its value any way intent (out): the dummy argument only returns information from the procedure to the calling program intent (inout): the dummy argument provides information in both directions

    26. Examples of subprograms Write a program using either subroutine or function: read the edges of a rectangle, write a subprogram which calculate area of rectangle

    49. Examples of subprograms Write a program using either subroutine or function: read the edges of a rectangle, write a subprogram which calculate area of rectangle