SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Algorithm and Programming
Saniati
saniati@teknokrat .ac.id
Objective of
this session
 Student able to know definition dan theories of function.
 Student able to use and create functions.
 Student able to know theories of variable scope.
 Student able to identify the scope of variable.
Function
• A function is a block of code that performs a task
• A function is a type of procedure or routine.
• Some program contains at least one function: main()
• Why use functions?
▫ Allow programmer to avoid duplicating code
▫ Allow programs to be broken into manageable
tasks
• Two categories of function:
▫ Value-returning functions
▫ Void functions
Value –
Returning
Function
• A value-returning function returns precisely one value
after completing its assigned task.
▫ Usually returned to the statement that called function
 Typically, statement displays return value, uses it in a
calculation, or assigns it to a variable
▫ Some value-returning functions
 E.g., pow(3,2)
Value –
Returning
Function
• A value-returning function returns precisely one value
after completing its assigned task.
▫ Usually returned to the statement that called function
 Typically, statement displays return value, uses it in a
calculation, or assigns it to a variable
▫ Some value-returning functions
 E.g., pow(3,2)
Example
int
Void Function
void sayHello(string name)
{
write (“Halloooo “ + name );
}
Without
Return
Components
of Function
Function
Header
Function
Body
Function
Header
• Function header: first line in a function definition
▫ Does not end with a semicolon
▫ Begins with returnDataType
▫ Next comes the name of the function
 Naming rules are the same as for naming variables
 Name usually begins with a verb
▫ Also has an optional parameterList enclosed in ()
 Lists data type and name of formal parameters
 Formal parameters store the information passed to the function when it is
invoked
Formal
Parameter
Function
Body
• Function body contains instructions the function
follows to perform its assigned task
▫ Begins with { and ends with }
▫ The last statement is usually return expression;
 expression represents the value the function returns to the statement
that called it
▫ return statement alerts computer that the
function has completed its task
 Data type of expression must agree with the returnDataType specified
in function header
11
Call a function
(1)
void main(){
int a, b, c;
b = 5;
c = sum (a, b);
write (c);
}
int sum (int num1, int num2)
{
int result;
result = num1 + num2;
return (result);
}
Calling function
Variable, get the
return of function
Call a function
(2)
void main(){
int a , b, c;
a = 9; b = 5;
c = sum (a, b);
write (c);
}
int sum (int num1, int num2)
{
int result;
result = num1 + num2;
return (result);
}
Actual Parameter
Passing
Information to
a Function
• Items passed to a function are actual perameters/
arguments
▫ Can be a variable, named constant, literal constant, or
keyword
 You can pass a variable:
 by value (a copy of the value is passed), or
 by reference (the variable address is passed)
▫ Examples
 calcRectangleArea(2, 3)
 calcRectangleArea(length, width) pass by value
Using a
Function
Prototype
• If a function definition appears below main(), you must
enter a function prototype above main()
▫ Prototype specifies function’s name, data type of
return value and of each formal parameter
16
The Scope
and Lifetime
of a Variable
• A variable’s scope indicates where
in the program it can be used
▫ Local or global
 Local variables are declared within a
function or appear in a function’s
parameterList
 Global variables are declared outside
of any function and remain in memory
until program ends
 Avoid using global variables in your
programs
• A variable’s lifetime indicates how
long it remains in memory
Summary
 A function is a block of code that performs a task.
 Categories of function: Value-returning functions
& Void functions
 Components of function : Header Function &
Body Function
 Types of Parameter : Formal & Actual
 Next  Recursive Function

Weitere ähnliche Inhalte

Was ist angesagt? (20)

FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
user defined function
user defined functionuser defined function
user defined function
 
Functions
FunctionsFunctions
Functions
 
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Function in c++
Function in c++Function in c++
Function in c++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions and tasks in verilog
Functions and tasks in verilogFunctions and tasks in verilog
Functions and tasks in verilog
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Python recursion
Python recursionPython recursion
Python recursion
 
Python Built-in Functions and Use cases
Python Built-in Functions and Use casesPython Built-in Functions and Use cases
Python Built-in Functions and Use cases
 
4. function
4. function4. function
4. function
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and Functions
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
Function
Function Function
Function
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
functions of C++
functions of C++functions of C++
functions of C++
 
Inline function
Inline functionInline function
Inline function
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming
 
Unit 4
Unit 4Unit 4
Unit 4
 

Ähnlich wie Function

Ähnlich wie Function (20)

Functions
FunctionsFunctions
Functions
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Unit 8
Unit 8Unit 8
Unit 8
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Functions
FunctionsFunctions
Functions
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
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
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 

Kürzlich hochgeladen

Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡anilsa9823
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticssakshisoni2385
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfSumit Kumar yadav
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxUmerFayaz5
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)PraveenaKalaiselvan1
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksSérgio Sacani
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)Areesha Ahmad
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bSérgio Sacani
 
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPirithiRaju
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PPRINCE C P
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)Areesha Ahmad
 
❤Jammu Kashmir Call Girls 8617697112 Personal Whatsapp Number 💦✅.
❤Jammu Kashmir Call Girls 8617697112 Personal Whatsapp Number 💦✅.❤Jammu Kashmir Call Girls 8617697112 Personal Whatsapp Number 💦✅.
❤Jammu Kashmir Call Girls 8617697112 Personal Whatsapp Number 💦✅.Nitya salvi
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfSumit Kumar yadav
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...RohitNehra6
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINsankalpkumarsahoo174
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfrohankumarsinghrore1
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 

Kürzlich hochgeladen (20)

Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptx
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disks
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C P
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)
 
❤Jammu Kashmir Call Girls 8617697112 Personal Whatsapp Number 💦✅.
❤Jammu Kashmir Call Girls 8617697112 Personal Whatsapp Number 💦✅.❤Jammu Kashmir Call Girls 8617697112 Personal Whatsapp Number 💦✅.
❤Jammu Kashmir Call Girls 8617697112 Personal Whatsapp Number 💦✅.
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdf
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...
 
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATINChromatin Structure | EUCHROMATIN | HETEROCHROMATIN
Chromatin Structure | EUCHROMATIN | HETEROCHROMATIN
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdf
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 

Function

  • 2. Objective of this session  Student able to know definition dan theories of function.  Student able to use and create functions.  Student able to know theories of variable scope.  Student able to identify the scope of variable.
  • 3. Function • A function is a block of code that performs a task • A function is a type of procedure or routine. • Some program contains at least one function: main() • Why use functions? ▫ Allow programmer to avoid duplicating code ▫ Allow programs to be broken into manageable tasks • Two categories of function: ▫ Value-returning functions ▫ Void functions
  • 4. Value – Returning Function • A value-returning function returns precisely one value after completing its assigned task. ▫ Usually returned to the statement that called function  Typically, statement displays return value, uses it in a calculation, or assigns it to a variable ▫ Some value-returning functions  E.g., pow(3,2)
  • 5. Value – Returning Function • A value-returning function returns precisely one value after completing its assigned task. ▫ Usually returned to the statement that called function  Typically, statement displays return value, uses it in a calculation, or assigns it to a variable ▫ Some value-returning functions  E.g., pow(3,2)
  • 7. Void Function void sayHello(string name) { write (“Halloooo “ + name ); } Without Return
  • 9. Function Header • Function header: first line in a function definition ▫ Does not end with a semicolon ▫ Begins with returnDataType ▫ Next comes the name of the function  Naming rules are the same as for naming variables  Name usually begins with a verb ▫ Also has an optional parameterList enclosed in ()  Lists data type and name of formal parameters  Formal parameters store the information passed to the function when it is invoked Formal Parameter
  • 10. Function Body • Function body contains instructions the function follows to perform its assigned task ▫ Begins with { and ends with } ▫ The last statement is usually return expression;  expression represents the value the function returns to the statement that called it ▫ return statement alerts computer that the function has completed its task  Data type of expression must agree with the returnDataType specified in function header
  • 11. 11
  • 12. Call a function (1) void main(){ int a, b, c; b = 5; c = sum (a, b); write (c); } int sum (int num1, int num2) { int result; result = num1 + num2; return (result); } Calling function Variable, get the return of function
  • 13. Call a function (2) void main(){ int a , b, c; a = 9; b = 5; c = sum (a, b); write (c); } int sum (int num1, int num2) { int result; result = num1 + num2; return (result); } Actual Parameter
  • 14. Passing Information to a Function • Items passed to a function are actual perameters/ arguments ▫ Can be a variable, named constant, literal constant, or keyword  You can pass a variable:  by value (a copy of the value is passed), or  by reference (the variable address is passed) ▫ Examples  calcRectangleArea(2, 3)  calcRectangleArea(length, width) pass by value
  • 15. Using a Function Prototype • If a function definition appears below main(), you must enter a function prototype above main() ▫ Prototype specifies function’s name, data type of return value and of each formal parameter
  • 16. 16
  • 17. The Scope and Lifetime of a Variable • A variable’s scope indicates where in the program it can be used ▫ Local or global  Local variables are declared within a function or appear in a function’s parameterList  Global variables are declared outside of any function and remain in memory until program ends  Avoid using global variables in your programs • A variable’s lifetime indicates how long it remains in memory
  • 18. Summary  A function is a block of code that performs a task.  Categories of function: Value-returning functions & Void functions  Components of function : Header Function & Body Function  Types of Parameter : Formal & Actual  Next  Recursive Function