SlideShare a Scribd company logo
SUBPROGRAM
1
SUBPROGRAM
Topics:
 Definitions of subprogram
 general subprogram characteristics
 parameters
 Functions and procedures
 Design issues for subprogram
 Parameter passing method
 Model for parameter passing
 Overload subprogram
 Type checking & referencing environment
 Subprogram passed method
 Generic subprogram
2
Definitions :
Subprogram :
A program separate from the main program that executes aseries
Of operations that occures multiple times during the machine cycle.
Subprogram : describes the interface to and the actions of the subprogram
abstraction .
Subprogram call : is the explicit request that the called subprograms be
executed .
Process abstraction are presented in programming languages by
subprograms
3
What does it mean for a subproram to be active ?
Subprogram : is to be active if after having been called ,it has
begun execution but has not yet completed that execution
Execution time
Duration time
Subprogram(x) start
4
Definitions :
A Subprogram header :
is the first line of the definition ,serves several purposes. First is
specifies that the following syntactic unit is a subprogram's kind
is often accomplished with a special word .
The header contains some keyword signalin the beginning of
asubproram, a name for the subprogram ,and Header it may
optionally specify alist of parameters
e.g: (Fortran)
Subroutine Adder (parameters) 5
e.g: (C)
Void adder (parameters)
,would serve as the header of a SUBPROGRAM named
adder ,where void indicates that it does not return a value
Prototype :is a subprogram declaration providing
Interface information
Definitions :
6
Definitions :
Parameter profile:
Describes the number ,order , and types of the parameters
**also called “signature”.
7
General subprogram characteristics :
 Each subprogram has a single entry point
 The calling program unit is suspend during The execution of the called
subprogram , which means that there is only one subprogram in
execution at any time
 Control always return to the caller when the subprogram execution
terminates
8
Parameters :
There are two ways that subprogram can gain access to the data
that it is to process:
Direct access to nonlocal variables
Parameter passing
** Parameter passing is more flexible than direct access to
nonlocal variables
9
 Formal parameters :
The parameters in the subprogram header
 Actual parameters :
alist of parameters that appear in subprogram call statement
 Positional parameters:
is a method for binding actual parameter to formal parameter, is done by
position
** ((the first actual parameters is bound to the first formal parameter and so
forth such parameters are called positional parameters ))
e.g :
M = rect (a , b ,c ) // subproram call
Float rect ( int x , int y , int z )
Parameters : (count …)
10
Keyword parameters :
The name of the formal parameter to which an actual parameter is to be
bound is specified with the actual parameter
Advantage:
That they can appear in any order in the actual parameter list
Disadvantage:
That the user of the subprogram must know the names of formal
parameters
Parameters : (count …)
11
Subprogram example:
Int cube(int); prototype
Int main(){
Int y=5; actual parameters
Cout<<cube(y); Subprogram call
Int x=3;
Int cube (int x); subprogram header
{
formal parameter
return x*x;
}
}
12
The Two kinds of subprograms :
There are two distinct categories of subprograms:
 Procedures
 Functions
13
Procedures :
Procedures :
are collection of statements that define parameterized computations ,
these computations are enacted by single call statements.
Procedure are called subroutines
Procedures has Two parts :
The speification and the Body
The specification is begins with the keyword PROCEDURE and ends
With the procedure name or parameter list
e.G : PROCEDURE a_test(a,b : in integer ; c:out Integer) 14
Procedures :
Procedure identifier Rules :
The name must start with aletter or made out of letters
, numbers and the underscore( _ ).
 the name is always case-sensetive ( uppercase / lowercase
names are identical).
 the name may not start with the dollar-sign ($).
15
Examples of procedures
Program example1;
Specification
procedure add;
var
x : integer ;
y : integer ;
begin BODY
read ( x , y );
write ( x + y );
end;
BEGIN
add;
END .
Program example2 ;
Var
x : integer ;
y : integer ;
Procedure add ( a : integer ; b : integer
);
begin
write ( a + b );
end;
BEGIN
x = 10 ;
y = 5 ;
add ( 10 , 5 ) ;
END.
16
Functions :
Functions : structurally resemble procedure, But are semantically
modeled on mathematical functions ( Functions are procedures
which return value).
** Function are called by appearances of their name in expressions
e.g: (function header and call )
Void sort (int list[], int listlen); // function header
…
Sort(scores,100); // function call
17
Functions :
** In function body The return statement end the execution of
the function and return the value of the expression enclosed
In the braces as a result
**The function body can contain any number of return
statement
18
Functions :
// function example :
Function minimum ( A,B : Integer) return integer is
Begin
If A<= B then
Return A ;
Else
Return B ;
End if ;
End minimum;
19
Design Issues For Functions :
There are Two design issues are specific to functions :
 Functional side effect
The evaluation of a function effect the the value of other operands in the same
expression.
** (solution parameters to functios can have only in mode formal parameter)
 Types of returned value
most imperative programming languages restrict the types that can be returned by
their functions .
 C allow any type to be returned by its function except arrays and functions “both
of these can be handled by pointer type return values ”
 Ada language its function can return values of any type
20
QUESTIONS?
21
Design issues for subprogram :
 What parameter passing method use ?
 Are the type of the actual parameter checked against the formal
parameter ?
 Are local variables statically or dynamically allocated?
 If subprogram can be passed as parameters what is the referencing
environment of such subprogram ?
Can subprogram be overloaded ?
Can subprogram be generic ?
Is either separate or independent compilation possible ?
22
Parameter passing methods :
 Parameter passing methods are the ways in which parameters are
transmitted to and/or from calling subprogram,
 Formal parameters are characterized by one of three distinct
semantics models :
1. They can receive data from the corresponding actual parameter (in
mode).
2. They can transmit data to the actual parameter (out mode).
3. They can do both (inout mode).
23
Graph for parameter passing methods :
24
Implementation models for parameter passing :
 Pass by value
 Pass by result
 Pass by value result
 Pass by reference
 Pass by name
25
Pass by value :
When parameter is passed by value ,the value of the actual parameter is used
to initialize the correspondin formal parameter ,which then act as alocal
variable in the subprogram
 Advantage: simple mechanism,actual parameter can be expression or
values, (formal parameters are write protected) does not cause side effect.
 Disadvantage: needs additional storage for formal parameters .
26
Pass by result :
 Formal parameter acts as local variable just before control returns to caller ,
value is passed to actual parameter , which must be a variable
 problem #1:
Actual parameter collision , such as call sub(p1,p1) may return different values to
p1 depending on order of assignment
 Problem #2:
Address evaluation may be done at call or at return, for example list[index] if the
index is changed by the subprogram ,either through global access or as formal
parameter ,ten the address of list[index] will changed between the call and the
return
27
Pass by value result :
 The value of the actual parameter is used to initialize the corresponding
formal parameter ,the value of the formal parameter is transmitted back
to the actual parameter
 Pass by value result is some times call pass by copy
 Pass by value result share with pass by value and pass by result the
disadvantage of requiring multiple storage for parameters and time for
copying values .
 Pass by value result share with pass by result the problems associated
with the order in which actual parameters are assigned .
28
Pass by reference :
 In pass by reference method transmits an access path ,usually just an
address , to the called subprogram
 The called subprogram is allowed to access the actual parameter in the
calling program unit .
29
Pass by reference : (count …)
 The advantage of Pass by reference :
Passing process is efficient , in term of both time and space , duplicate space is not
required , nor is any copying required .
 The disadvantage of Pass by reference :
1. access to the formal parameters will be slower ,because of the additional level of
indirect addressing that is required
2. if only one way communication to the called subprogram is required ,inadvertent and
erroneous changes may be made to the actual parameter
3. the aliases can be created
30
Pass by name :
 The actual parameter is textually substituted for the formal parameter in
all its occurrences in the subprogram .
Advantage:
 More flexible than other methods.
Disadvantage :
 Relatively slow than other methods
 Very expensive
31
Parameter passing example:
Program ParamPassing;
var
i : integer;
a : array[1..2] of integer;
procedure p(x, y : integer);
begin
x := x + 1;
i := i + 1;
y := y + 1;
end; {p}
begin {ParamPassing}
a[1] := 1;
a[2] := 1;
i := 1;
p(a[i], a[i]);
writeln(‘a[1] = ’, a[1]);
writeln(‘a[2] = ’, a[2]);
end.
Pass by value:
a[1] = 1
a[2] = 1
Pass by result:
x and y have no initial values
Pass by value-result:
a[1] = 2
a[2] = 1
Pass by reference:
a[1] = 3
a[2] = 1
Pass by name:
a[1] = 2
a[2] = 2
32
Overload Subprogram
 Overload Subprogram is a subprogram that has the same name as
another subprogram in the same referencing environment
 Overloaded subprograms that have default parameters can lead to
ambiguous subprogram calls.
 Example: (C++)
void fun( float b = 0.0 );
void fun();
…
fun ( );
33
QUESTIONS?
34
Type checking parameter :
Without type checking ,some typographical errors lead to program
error difficult to diagnose because they are not detected by the
compiler or the run time system.
Early programming language ,such as FORTRAN 77 and the
original version of c did not required parameter type checking
,most later language require it , the relatively recent language Perl,
JavaScript , and PHP do not
35
Example of type checking parameter :
double sin(x)
double x;
{ …. }
Using this method avoids type checking, thereby allowing calls such as:
Double value;
int count;
…
Value = sin (count);
To be legal, although they are never correct
36
Subprogram passed as parameters:
Some language allow nested subprogram ((allow subprogram
to be a parameter for another subprogram))
So in this case the following choices for referencing
environment:
1. Shallow binding : the environment of the call statement
that enacts the passed subprogram.
2. Deep binding : the environment of the definition
of the passed subprogram .
3. Ad hoc binding : the environment of the call statement that
passed the subprogram as an actual parameter 37
Generic Subprogram
 A generic or polymorphic subprogram is one that takes parameters
of different types on different activations.
 Overloaded subprograms provide ad hoc polymorphism .
 A subprogram that takes a generic parameter that is used in a type
expression that describes the type of the parameters of the
subprogram provides parametric polymorphism .
38
Advantages to using subprograms
There are several advantages to using subprograms:
They help keep the code simple, and, thus, more readable;
They allow the programmer to use the same code as many times as needed
throughout the program;
They allow the programmer to define needed functions; and,
They can be used in other programs
39
THANK YOU
40

More Related Content

What's hot

Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
Md. Tanvir Hossain
 
Built in function
Built in functionBuilt in function
Built in function
MD. Rayhanul Islam Sayket
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Quality and productivity factors
Quality and productivity factorsQuality and productivity factors
Quality and productivity factors
NancyBeaulah_R
 
Functional and non functional
Functional and non functionalFunctional and non functional
Functional and non functional
Dikshyanta Dhungana
 
Software Cost Estimation Techniques
Software Cost Estimation TechniquesSoftware Cost Estimation Techniques
Software Cost Estimation Techniques
Santhi thi
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
baran19901990
 
Fundamental design concepts
Fundamental design conceptsFundamental design concepts
Fundamental design concepts
srijavel
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
Ramakant Soni
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
source code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniquessource code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniques
Siva Priya
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
Gayathri Ganesh
 
Chapter 1
Chapter 1Chapter 1
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
Hashim Hashim
 

What's hot (20)

Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Built in function
Built in functionBuilt in function
Built in function
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
class and objects
class and objectsclass and objects
class and objects
 
Quality and productivity factors
Quality and productivity factorsQuality and productivity factors
Quality and productivity factors
 
interface in c#
interface in c#interface in c#
interface in c#
 
Functional and non functional
Functional and non functionalFunctional and non functional
Functional and non functional
 
Software Cost Estimation Techniques
Software Cost Estimation TechniquesSoftware Cost Estimation Techniques
Software Cost Estimation Techniques
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Fundamental design concepts
Fundamental design conceptsFundamental design concepts
Fundamental design concepts
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
source code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniquessource code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniques
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 

Similar to Subprogramms

Unit 2
Unit 2Unit 2
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
Ahmad Kamal
 
Function C programming
Function C programmingFunction C programming
Function C programming
Appili Vamsi Krishna
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
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
SangeetaBorde3
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
Functions in c language1
Functions in c language1Functions in c language1
Functions in c language1
sirikeshava
 
Lecture11 abap on line
Lecture11 abap on lineLecture11 abap on line
Lecture11 abap on lineMilind Patil
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
atishupadhyay
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
nmahi96
 
Functions
Functions Functions
Functions
Praneeth960856
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
 
Functions
FunctionsFunctions
Functions
FunctionsFunctions
Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...
Sar
 
Unit 1
Unit  1Unit  1
Unit 1
donny101
 
Presentation of computer
Presentation of computerPresentation of computer
Presentation of computer
SabinDhakal13
 

Similar to Subprogramms (20)

Unit 2
Unit 2Unit 2
Unit 2
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
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
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Functions in c language1
Functions in c language1Functions in c language1
Functions in c language1
 
Lecture11 abap on line
Lecture11 abap on lineLecture11 abap on line
Lecture11 abap on line
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
Functions
Functions Functions
Functions
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...
 
Unit 1
Unit  1Unit  1
Unit 1
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
Presentation of computer
Presentation of computerPresentation of computer
Presentation of computer
 

Recently uploaded

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 

Recently uploaded (20)

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 

Subprogramms

  • 2. SUBPROGRAM Topics:  Definitions of subprogram  general subprogram characteristics  parameters  Functions and procedures  Design issues for subprogram  Parameter passing method  Model for parameter passing  Overload subprogram  Type checking & referencing environment  Subprogram passed method  Generic subprogram 2
  • 3. Definitions : Subprogram : A program separate from the main program that executes aseries Of operations that occures multiple times during the machine cycle. Subprogram : describes the interface to and the actions of the subprogram abstraction . Subprogram call : is the explicit request that the called subprograms be executed . Process abstraction are presented in programming languages by subprograms 3
  • 4. What does it mean for a subproram to be active ? Subprogram : is to be active if after having been called ,it has begun execution but has not yet completed that execution Execution time Duration time Subprogram(x) start 4
  • 5. Definitions : A Subprogram header : is the first line of the definition ,serves several purposes. First is specifies that the following syntactic unit is a subprogram's kind is often accomplished with a special word . The header contains some keyword signalin the beginning of asubproram, a name for the subprogram ,and Header it may optionally specify alist of parameters e.g: (Fortran) Subroutine Adder (parameters) 5
  • 6. e.g: (C) Void adder (parameters) ,would serve as the header of a SUBPROGRAM named adder ,where void indicates that it does not return a value Prototype :is a subprogram declaration providing Interface information Definitions : 6
  • 7. Definitions : Parameter profile: Describes the number ,order , and types of the parameters **also called “signature”. 7
  • 8. General subprogram characteristics :  Each subprogram has a single entry point  The calling program unit is suspend during The execution of the called subprogram , which means that there is only one subprogram in execution at any time  Control always return to the caller when the subprogram execution terminates 8
  • 9. Parameters : There are two ways that subprogram can gain access to the data that it is to process: Direct access to nonlocal variables Parameter passing ** Parameter passing is more flexible than direct access to nonlocal variables 9
  • 10.  Formal parameters : The parameters in the subprogram header  Actual parameters : alist of parameters that appear in subprogram call statement  Positional parameters: is a method for binding actual parameter to formal parameter, is done by position ** ((the first actual parameters is bound to the first formal parameter and so forth such parameters are called positional parameters )) e.g : M = rect (a , b ,c ) // subproram call Float rect ( int x , int y , int z ) Parameters : (count …) 10
  • 11. Keyword parameters : The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter Advantage: That they can appear in any order in the actual parameter list Disadvantage: That the user of the subprogram must know the names of formal parameters Parameters : (count …) 11
  • 12. Subprogram example: Int cube(int); prototype Int main(){ Int y=5; actual parameters Cout<<cube(y); Subprogram call Int x=3; Int cube (int x); subprogram header { formal parameter return x*x; } } 12
  • 13. The Two kinds of subprograms : There are two distinct categories of subprograms:  Procedures  Functions 13
  • 14. Procedures : Procedures : are collection of statements that define parameterized computations , these computations are enacted by single call statements. Procedure are called subroutines Procedures has Two parts : The speification and the Body The specification is begins with the keyword PROCEDURE and ends With the procedure name or parameter list e.G : PROCEDURE a_test(a,b : in integer ; c:out Integer) 14
  • 15. Procedures : Procedure identifier Rules : The name must start with aletter or made out of letters , numbers and the underscore( _ ).  the name is always case-sensetive ( uppercase / lowercase names are identical).  the name may not start with the dollar-sign ($). 15
  • 16. Examples of procedures Program example1; Specification procedure add; var x : integer ; y : integer ; begin BODY read ( x , y ); write ( x + y ); end; BEGIN add; END . Program example2 ; Var x : integer ; y : integer ; Procedure add ( a : integer ; b : integer ); begin write ( a + b ); end; BEGIN x = 10 ; y = 5 ; add ( 10 , 5 ) ; END. 16
  • 17. Functions : Functions : structurally resemble procedure, But are semantically modeled on mathematical functions ( Functions are procedures which return value). ** Function are called by appearances of their name in expressions e.g: (function header and call ) Void sort (int list[], int listlen); // function header … Sort(scores,100); // function call 17
  • 18. Functions : ** In function body The return statement end the execution of the function and return the value of the expression enclosed In the braces as a result **The function body can contain any number of return statement 18
  • 19. Functions : // function example : Function minimum ( A,B : Integer) return integer is Begin If A<= B then Return A ; Else Return B ; End if ; End minimum; 19
  • 20. Design Issues For Functions : There are Two design issues are specific to functions :  Functional side effect The evaluation of a function effect the the value of other operands in the same expression. ** (solution parameters to functios can have only in mode formal parameter)  Types of returned value most imperative programming languages restrict the types that can be returned by their functions .  C allow any type to be returned by its function except arrays and functions “both of these can be handled by pointer type return values ”  Ada language its function can return values of any type 20
  • 22. Design issues for subprogram :  What parameter passing method use ?  Are the type of the actual parameter checked against the formal parameter ?  Are local variables statically or dynamically allocated?  If subprogram can be passed as parameters what is the referencing environment of such subprogram ? Can subprogram be overloaded ? Can subprogram be generic ? Is either separate or independent compilation possible ? 22
  • 23. Parameter passing methods :  Parameter passing methods are the ways in which parameters are transmitted to and/or from calling subprogram,  Formal parameters are characterized by one of three distinct semantics models : 1. They can receive data from the corresponding actual parameter (in mode). 2. They can transmit data to the actual parameter (out mode). 3. They can do both (inout mode). 23
  • 24. Graph for parameter passing methods : 24
  • 25. Implementation models for parameter passing :  Pass by value  Pass by result  Pass by value result  Pass by reference  Pass by name 25
  • 26. Pass by value : When parameter is passed by value ,the value of the actual parameter is used to initialize the correspondin formal parameter ,which then act as alocal variable in the subprogram  Advantage: simple mechanism,actual parameter can be expression or values, (formal parameters are write protected) does not cause side effect.  Disadvantage: needs additional storage for formal parameters . 26
  • 27. Pass by result :  Formal parameter acts as local variable just before control returns to caller , value is passed to actual parameter , which must be a variable  problem #1: Actual parameter collision , such as call sub(p1,p1) may return different values to p1 depending on order of assignment  Problem #2: Address evaluation may be done at call or at return, for example list[index] if the index is changed by the subprogram ,either through global access or as formal parameter ,ten the address of list[index] will changed between the call and the return 27
  • 28. Pass by value result :  The value of the actual parameter is used to initialize the corresponding formal parameter ,the value of the formal parameter is transmitted back to the actual parameter  Pass by value result is some times call pass by copy  Pass by value result share with pass by value and pass by result the disadvantage of requiring multiple storage for parameters and time for copying values .  Pass by value result share with pass by result the problems associated with the order in which actual parameters are assigned . 28
  • 29. Pass by reference :  In pass by reference method transmits an access path ,usually just an address , to the called subprogram  The called subprogram is allowed to access the actual parameter in the calling program unit . 29
  • 30. Pass by reference : (count …)  The advantage of Pass by reference : Passing process is efficient , in term of both time and space , duplicate space is not required , nor is any copying required .  The disadvantage of Pass by reference : 1. access to the formal parameters will be slower ,because of the additional level of indirect addressing that is required 2. if only one way communication to the called subprogram is required ,inadvertent and erroneous changes may be made to the actual parameter 3. the aliases can be created 30
  • 31. Pass by name :  The actual parameter is textually substituted for the formal parameter in all its occurrences in the subprogram . Advantage:  More flexible than other methods. Disadvantage :  Relatively slow than other methods  Very expensive 31
  • 32. Parameter passing example: Program ParamPassing; var i : integer; a : array[1..2] of integer; procedure p(x, y : integer); begin x := x + 1; i := i + 1; y := y + 1; end; {p} begin {ParamPassing} a[1] := 1; a[2] := 1; i := 1; p(a[i], a[i]); writeln(‘a[1] = ’, a[1]); writeln(‘a[2] = ’, a[2]); end. Pass by value: a[1] = 1 a[2] = 1 Pass by result: x and y have no initial values Pass by value-result: a[1] = 2 a[2] = 1 Pass by reference: a[1] = 3 a[2] = 1 Pass by name: a[1] = 2 a[2] = 2 32
  • 33. Overload Subprogram  Overload Subprogram is a subprogram that has the same name as another subprogram in the same referencing environment  Overloaded subprograms that have default parameters can lead to ambiguous subprogram calls.  Example: (C++) void fun( float b = 0.0 ); void fun(); … fun ( ); 33
  • 35. Type checking parameter : Without type checking ,some typographical errors lead to program error difficult to diagnose because they are not detected by the compiler or the run time system. Early programming language ,such as FORTRAN 77 and the original version of c did not required parameter type checking ,most later language require it , the relatively recent language Perl, JavaScript , and PHP do not 35
  • 36. Example of type checking parameter : double sin(x) double x; { …. } Using this method avoids type checking, thereby allowing calls such as: Double value; int count; … Value = sin (count); To be legal, although they are never correct 36
  • 37. Subprogram passed as parameters: Some language allow nested subprogram ((allow subprogram to be a parameter for another subprogram)) So in this case the following choices for referencing environment: 1. Shallow binding : the environment of the call statement that enacts the passed subprogram. 2. Deep binding : the environment of the definition of the passed subprogram . 3. Ad hoc binding : the environment of the call statement that passed the subprogram as an actual parameter 37
  • 38. Generic Subprogram  A generic or polymorphic subprogram is one that takes parameters of different types on different activations.  Overloaded subprograms provide ad hoc polymorphism .  A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism . 38
  • 39. Advantages to using subprograms There are several advantages to using subprograms: They help keep the code simple, and, thus, more readable; They allow the programmer to use the same code as many times as needed throughout the program; They allow the programmer to define needed functions; and, They can be used in other programs 39