SlideShare a Scribd company logo
1 of 18
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

What's hot

User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design IntroductionKuppusamy P
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloadingBalajiGovindan5
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++Neeru Mittal
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Storage class in c
Storage class in cStorage class in c
Storage class in ckash95
 

What's hot (20)

User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Inline function
Inline functionInline function
Inline function
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Linker and Loader
Linker and Loader Linker and Loader
Linker and Loader
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Shift rotate
Shift rotateShift rotate
Shift rotate
 
Data types in c++
Data types in c++ Data types in c++
Data types in c++
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
Run time storage
Run time storageRun time storage
Run time storage
 

Viewers also liked

Subroutine in 8051 microcontroller
Subroutine in 8051 microcontrollerSubroutine in 8051 microcontroller
Subroutine in 8051 microcontrollerbhadresh savani
 
Stacks & subroutines 1
Stacks & subroutines 1Stacks & subroutines 1
Stacks & subroutines 1deval patel
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutineAshim Saha
 
Subroutine & string in 8086 Microprocessor
Subroutine & string in 8086 MicroprocessorSubroutine & string in 8086 Microprocessor
Subroutine & string in 8086 MicroprocessorMustafa AL-Timemmie
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directivesSARITHA REDDY
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Safin Biswas
 
8051 Microcontroller Notes
8051 Microcontroller Notes8051 Microcontroller Notes
8051 Microcontroller NotesDr.YNM
 
1347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 80511347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 8051techbed
 

Viewers also liked (8)

Subroutine in 8051 microcontroller
Subroutine in 8051 microcontrollerSubroutine in 8051 microcontroller
Subroutine in 8051 microcontroller
 
Stacks & subroutines 1
Stacks & subroutines 1Stacks & subroutines 1
Stacks & subroutines 1
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Subroutine & string in 8086 Microprocessor
Subroutine & string in 8086 MicroprocessorSubroutine & string in 8086 Microprocessor
Subroutine & string in 8086 Microprocessor
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directives
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)
 
8051 Microcontroller Notes
8051 Microcontroller Notes8051 Microcontroller Notes
8051 Microcontroller Notes
 
1347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 80511347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 8051
 

Similar to 16 subroutine

UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functionsnikshaikh786
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)Arpit Meena
 
User defined functions
User defined functionsUser defined functions
User defined functionsshubham_jangid
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...anaveenkumar4
 

Similar to 16 subroutine (20)

UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
functions
functionsfunctions
functions
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Functions
Functions Functions
Functions
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
Functions.docx
Functions.docxFunctions.docx
Functions.docx
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Functions
FunctionsFunctions
Functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...04_python_functions.ppt You can define functions to provide the required func...
04_python_functions.ppt You can define functions to provide the required func...
 

More from fyjordan9

More from fyjordan9 (17)

17recursion
17recursion17recursion
17recursion
 
15 functions
15 functions15 functions
15 functions
 
14 arrays
14 arrays14 arrays
14 arrays
 
13 arrays
13 arrays13 arrays
13 arrays
 
12 doloops
12 doloops12 doloops
12 doloops
 
11 doloops
11 doloops11 doloops
11 doloops
 
10 examples for if statement
10 examples for if statement10 examples for if statement
10 examples for if statement
 
9 case
9 case9 case
9 case
 
8 if
8 if8 if
8 if
 
7 files
7 files7 files
7 files
 
6 read write
6 read write6 read write
6 read write
 
5 format
5 format5 format
5 format
 
4 design
4 design4 design
4 design
 
3 in out
3 in out3 in out
3 in out
 
2 int real
2 int real2 int real
2 int real
 
1 arithmetic
1 arithmetic1 arithmetic
1 arithmetic
 
PHYS303
PHYS303PHYS303
PHYS303
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.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