SlideShare a Scribd company logo
Basic Scientific Programming
Subprograms
Subroutines


Subroutines have many features in common
with functions:






They are program units designed to perform a
particular task, under the control of some other
program unit.
They have the same basic form: each consists of a
heading, a specification part, and execution part,
and an END statement.
The scope rules apply to both functions and
subroutines.


Functions and subroutines have the following
differences:






Functions are designed to return a single value to
the program unit that references them.
Subroutines often return more than one value, or
they may return no value at all.
Functions return values via function names,
subroutines return values via arguments
A function is referenced by using its name in an
expression, subroutines are referenced by a call
statement.
Subroutine Structure


The subroutine is structured as follows:





Subroutine heading
Specification part.
Execution part
End subroutine statement

Subroutine subroutine_name(formal_argument_list)

We use the heading to name the subroutine and
declare its arguments.
Ex:


Polar coordinates to rectangular coordinates:
the subprogram takes two arguments as input and
returns two values.

Subroutine polar_to_rect(R, theta, X,Y)
real, intent(in):: R, theta
read, intent(out):: X,Y
X= R * cos(theta)
Y= R * sin(theta)
End subroutine polar_to_rect
Call Statement


A subroutine is referenced by a CALL
statement.

Form

Call Subroutine_name(actual_argument_list)


Each actual argument must agree in
type with the corresponding formal
argument.
Call Statement




If there are no actual arguments, the
parentheses in the subroutine reference
may be omitted.
A function is referenced by using its
name in an expression, subroutines are
referenced by a call statement.
Argument Association


When the call statement

Call polar_to_rect(RC,TC,XC,YC)
is executed, the values of the actual
arguments RC and TC are passed to the
formal arguments R and Theta Respectively.
RC  R
TC  Theta






R and Theta are declared to be IN arguments
because the intent is that values are to be
passed and then used within the subroutine.
X and Y are declared to have the Intent(out)
attribute because they are intended only to
pass values back to the calling program.
(XC, YC)
A formal argument may have Intent(inout).


Subroutine calculate(alpha,beta,gamma)
real,intent(in):: alpha
integer,intent(out)::beta
integer,intent(inout):: gamma
…
end subroutine calculate
Assume the following declarations in the main
program
integer:: code, id_num
real:: rate


Tell if the following is a valid reference or
not:
rate = calculate(2.45,code,id_num)
call calculate(rate+0.5,0, code 2 id_num)
call calculate(rate, id_num)
call calculate(rate,code,id_num)
call calculate
call calculate(rate,rate,rate)
call calculate(code,code,code)
Optional Arguments of Subprograms






In all of our examples of subprograms, the number
and type of actual arguments in a subprogram
reference have matched the number of the formal
arguments in the subprogram heading.
In some cases, however, it is possible to design a
subprogram in which there may be fewer actual
arguments in a reference to a subprogram than there
are formal arguments.
This is accomplished by specifying that some
arguments are optional.
Optional Arguments


Ex: A + BX + CX2 + DX3 + EX4
coefficients A,B,C,D, and E are real constants.
function polynomial(x,a,b,c,d,e)
real:: polynomial
real,intent(in):: x,a,b,c,d,e

polynomial = a+b*x+c*x**2+d*x **3 + e*x**4
End function polynomial


Polynomial(2.5,1.5,2.0,3.0,-1,7.2)
is used to calculate
1.5 +2.0 X +3.0 X2 –X3 +7.2 X4 at X=2.5



What about 1 + 2X at X = 3.3

Polynomial(3.3,1.0,2.0,0.0,0.0,0.0)

note that it is necessary to supply zero
coefficients for any term that does not appear
in the polynomial


An alternative is to specify that the argument
specifying the coefficients b,c,d, and e are
optional by using the OPTIONAL specifier:
function polynomial(x,a,b,c,d,e)
real:: polynomial
real,intent(in)::x,a
real,intent(in),optional :: b,c,d,e

polynomial = a+b*x+c*x**2+d*x **3 + e*x**4
End function polynomial
Keyword Argument


The association of actual arguments
with formal arguments can also be
given explicitly by using Keyword
Arguments.
Formal_argument= actual_argument
Ex: polynomial(x=2.5, a = 2.0, e = 1.0)
polynomial(e=1.0, a = 2.0, x = 2.5)


The subprogram can use the predefined
function Present() to determine which
arguments have been supplied.
Present( formal_argument)



The return value is true if a value was
supplied for the formal_argument and false
otherwise.


Subroutine polynomial(result,x,a,b,c,d,e)
real,intent(out):: result
real, intent(in):: x,a
real, intent(in):: b,c,d,e
result = a
if(present(b)) result = result + b*x
if(present(c)) result = result + c*x **2
if(present(d)) result = result + d*x**3
if(present(e)) result = result + e*x**4
end subroutine polynomial

More Related Content

PDF
Daa notes 1
PPT
RECURSION IN C
PDF
Verilog Tasks & Functions
PPTX
Behavioral modelling in VHDL
PPTX
functions of C++
PPTX
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
PPTX
Hamming code system
PDF
Concepts of Behavioral modelling in Verilog HDL
Daa notes 1
RECURSION IN C
Verilog Tasks & Functions
Behavioral modelling in VHDL
functions of C++
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Hamming code system
Concepts of Behavioral modelling in Verilog HDL

What's hot (20)

PDF
C++ chapter 1
PPTX
Looping statements in C
PPTX
Presentation on C Switch Case Statements
PPTX
Flipflop
PPTX
Inline function in C++
PPTX
Functions in c language
PPTX
c++ programming Unit 2 basic structure of a c++ program
PPTX
Fundamentals of c programming
PPTX
Introduction of c programming
PDF
Assembly language 8086 intermediate
PPTX
Pn sequence
PDF
Differences between c and c++
PPTX
Input and Output In C Language
PPTX
Code Optimization
PPTX
NP completeness
PPTX
Cadence Design Flow.pptx
PDF
C++ Interview Question And Answer
PPT
358 33 powerpoint-slides_2-functions_chapter-2
PDF
Introduction to Arduino Programming
C++ chapter 1
Looping statements in C
Presentation on C Switch Case Statements
Flipflop
Inline function in C++
Functions in c language
c++ programming Unit 2 basic structure of a c++ program
Fundamentals of c programming
Introduction of c programming
Assembly language 8086 intermediate
Pn sequence
Differences between c and c++
Input and Output In C Language
Code Optimization
NP completeness
Cadence Design Flow.pptx
C++ Interview Question And Answer
358 33 powerpoint-slides_2-functions_chapter-2
Introduction to Arduino Programming
Ad

Viewers also liked (8)

PPTX
Subroutine in 8051 microcontroller
PPTX
Stacks & subroutines 1
PPT
Stack and subroutine
PPTX
Subroutine & string in 8086 Microprocessor
DOCX
8051 data types and directives
PPTX
Stack in microprocessor 8085(presantation)
DOC
8051 Microcontroller Notes
PPT
1347 Assembly Language Programming Of 8051
Subroutine in 8051 microcontroller
Stacks & subroutines 1
Stack and subroutine
Subroutine & string in 8086 Microprocessor
8051 data types and directives
Stack in microprocessor 8085(presantation)
8051 Microcontroller Notes
1347 Assembly Language Programming Of 8051
Ad

Similar to 16 subroutine (20)

PPT
15 functions
PPTX
Subroutines igcses computer science powerpoint
PDF
Programming withmatlab
PPTX
SAP Modularization techniques
PDF
modularization-160202092213 (1).pdf
PPTX
Part 3-functions1-120315220356-phpapp01
PDF
computational brrtyuuufdddfgggxxzzcv.pdf
PPTX
กลุ่ม6
PDF
Unit iii vb_study_materials
PPTX
Modularisation techniques new
PPT
Savitch ch 05
PDF
Section-6-User-Defined-Functions.pdf
PPT
Savitch Ch 05
PPT
Savitch Ch 05
DOCX
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
PDF
Lec16-CS110 Computational Engineering
PPT
Functions123
PPT
Functions12
PDF
Numerical Methods for Engineers 6th Edition Chapra Solutions Manual
15 functions
Subroutines igcses computer science powerpoint
Programming withmatlab
SAP Modularization techniques
modularization-160202092213 (1).pdf
Part 3-functions1-120315220356-phpapp01
computational brrtyuuufdddfgggxxzzcv.pdf
กลุ่ม6
Unit iii vb_study_materials
Modularisation techniques new
Savitch ch 05
Section-6-User-Defined-Functions.pdf
Savitch Ch 05
Savitch Ch 05
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
Lec16-CS110 Computational Engineering
Functions123
Functions12
Numerical Methods for Engineers 6th Edition Chapra Solutions Manual

More from fyjordan9 (16)

PPT
17recursion
PPT
14 arrays
PPT
13 arrays
PPT
12 doloops
PPT
11 doloops
PPT
10 examples for if statement
PPT
9 case
PPT
8 if
PPT
7 files
PPT
6 read write
PPT
5 format
PPT
4 design
PPT
3 in out
PPT
2 int real
PPT
1 arithmetic
PDF
PHYS303
17recursion
14 arrays
13 arrays
12 doloops
11 doloops
10 examples for if statement
9 case
8 if
7 files
6 read write
5 format
4 design
3 in out
2 int real
1 arithmetic
PHYS303

Recently uploaded (20)

PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Cell Types and Its function , kingdom of life
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Lesson notes of climatology university.
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Institutional Correction lecture only . . .
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Structure & Organelles in detailed.
Renaissance Architecture: A Journey from Faith to Humanism
human mycosis Human fungal infections are called human mycosis..pptx
Pre independence Education in Inndia.pdf
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Computing-Curriculum for Schools in Ghana
Cell Types and Its function , kingdom of life
Abdominal Access Techniques with Prof. Dr. R K Mishra
O7-L3 Supply Chain Operations - ICLT Program
Lesson notes of climatology university.
102 student loan defaulters named and shamed – Is someone you know on the list?
Institutional Correction lecture only . . .
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

16 subroutine

  • 2. Subroutines  Subroutines have many features in common with functions:    They are program units designed to perform a particular task, under the control of some other program unit. They have the same basic form: each consists of a heading, a specification part, and execution part, and an END statement. The scope rules apply to both functions and subroutines.
  • 3.  Functions and subroutines have the following differences:    Functions are designed to return a single value to the program unit that references them. Subroutines often return more than one value, or they may return no value at all. Functions return values via function names, subroutines return values via arguments A function is referenced by using its name in an expression, subroutines are referenced by a call statement.
  • 4. Subroutine Structure  The subroutine is structured as follows:     Subroutine heading Specification part. Execution part End subroutine statement Subroutine subroutine_name(formal_argument_list) We use the heading to name the subroutine and declare its arguments.
  • 5. Ex:  Polar coordinates to rectangular coordinates: the subprogram takes two arguments as input and returns two values. Subroutine polar_to_rect(R, theta, X,Y) real, intent(in):: R, theta read, intent(out):: X,Y X= R * cos(theta) Y= R * sin(theta) End subroutine polar_to_rect
  • 6. Call Statement  A subroutine is referenced by a CALL statement. Form Call Subroutine_name(actual_argument_list)  Each actual argument must agree in type with the corresponding formal argument.
  • 7. Call Statement   If there are no actual arguments, the parentheses in the subroutine reference may be omitted. A function is referenced by using its name in an expression, subroutines are referenced by a call statement.
  • 8. Argument Association  When the call statement Call polar_to_rect(RC,TC,XC,YC) is executed, the values of the actual arguments RC and TC are passed to the formal arguments R and Theta Respectively. RC  R TC  Theta
  • 9.    R and Theta are declared to be IN arguments because the intent is that values are to be passed and then used within the subroutine. X and Y are declared to have the Intent(out) attribute because they are intended only to pass values back to the calling program. (XC, YC) A formal argument may have Intent(inout).
  • 10.  Subroutine calculate(alpha,beta,gamma) real,intent(in):: alpha integer,intent(out)::beta integer,intent(inout):: gamma … end subroutine calculate Assume the following declarations in the main program integer:: code, id_num real:: rate
  • 11.  Tell if the following is a valid reference or not: rate = calculate(2.45,code,id_num) call calculate(rate+0.5,0, code 2 id_num) call calculate(rate, id_num) call calculate(rate,code,id_num) call calculate call calculate(rate,rate,rate) call calculate(code,code,code)
  • 12. Optional Arguments of Subprograms    In all of our examples of subprograms, the number and type of actual arguments in a subprogram reference have matched the number of the formal arguments in the subprogram heading. In some cases, however, it is possible to design a subprogram in which there may be fewer actual arguments in a reference to a subprogram than there are formal arguments. This is accomplished by specifying that some arguments are optional.
  • 13. Optional Arguments  Ex: A + BX + CX2 + DX3 + EX4 coefficients A,B,C,D, and E are real constants. function polynomial(x,a,b,c,d,e) real:: polynomial real,intent(in):: x,a,b,c,d,e polynomial = a+b*x+c*x**2+d*x **3 + e*x**4 End function polynomial
  • 14.  Polynomial(2.5,1.5,2.0,3.0,-1,7.2) is used to calculate 1.5 +2.0 X +3.0 X2 –X3 +7.2 X4 at X=2.5  What about 1 + 2X at X = 3.3 Polynomial(3.3,1.0,2.0,0.0,0.0,0.0) note that it is necessary to supply zero coefficients for any term that does not appear in the polynomial
  • 15.  An alternative is to specify that the argument specifying the coefficients b,c,d, and e are optional by using the OPTIONAL specifier: function polynomial(x,a,b,c,d,e) real:: polynomial real,intent(in)::x,a real,intent(in),optional :: b,c,d,e polynomial = a+b*x+c*x**2+d*x **3 + e*x**4 End function polynomial
  • 16. Keyword Argument  The association of actual arguments with formal arguments can also be given explicitly by using Keyword Arguments. Formal_argument= actual_argument Ex: polynomial(x=2.5, a = 2.0, e = 1.0) polynomial(e=1.0, a = 2.0, x = 2.5)
  • 17.  The subprogram can use the predefined function Present() to determine which arguments have been supplied. Present( formal_argument)  The return value is true if a value was supplied for the formal_argument and false otherwise.
  • 18.  Subroutine polynomial(result,x,a,b,c,d,e) real,intent(out):: result real, intent(in):: x,a real, intent(in):: b,c,d,e result = a if(present(b)) result = result + b*x if(present(c)) result = result + c*x **2 if(present(d)) result = result + d*x**3 if(present(e)) result = result + e*x**4 end subroutine polynomial