際際滷

際際滷Share a Scribd company logo
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 functions name, data type of
return value and of each formal parameter
16
The Scope
and Lifetime
of a Variable
 A variables scope indicates where
in the program it can be used
 Local or global
 Local variables are declared within a
function or appear in a functions
parameterList
 Global variables are declared outside
of any function and remain in memory
until program ends
 Avoid using global variables in your
programs
 A variables 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

More Related Content

What's hot (20)

FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
Krushal Kakadia
user defined function
user defined functionuser defined function
user defined function
King Kavin Patel
Functions
FunctionsFunctions
Functions
Septi Ratnasari
User Defined Functions in C
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
RAJ KUMAR
Scope of variables
Scope of variablesScope of variables
Scope of variables
baabtra.com - No. 1 supplier of quality freshers
Function in c++
Function in c++Function in c++
Function in c++
Kumar
Functions in c
Functions in cFunctions in c
Functions in c
reshmy12
Functions and tasks in verilog
Functions and tasks in verilogFunctions and tasks in verilog
Functions and tasks in verilog
Nallapati Anindra
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
Python recursion
Python recursionPython recursion
Python recursion
Prof. Dr. K. Adisesha
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
Srajan Mor
4. function
4. function4. function
4. function
Shankar Gangaju
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
Jay Baxi
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
Prabhu Govind
Function
Function Function
Function
Kathmandu University
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
Dr.Neeraj Kumar Pandey
functions of C++
functions of C++functions of C++
functions of C++
tarandeep_kaur
Inline function
Inline functionInline function
Inline function
Tech_MX
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming
GaurangVishnoi
Unit 4
Unit 4Unit 4
Unit 4
Vasavi College of Engg

Similar to Function (20)

Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
Functions
FunctionsFunctions
Functions
Golda Margret Sheeba J
Function C programming
Function C programmingFunction C programming
Function C programming
Appili Vamsi Krishna
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
Unit 8
Unit 8Unit 8
Unit 8
rohassanie
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
sumitbardhan
Functions
FunctionsFunctions
Functions
Pragnavi Erva
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
Hattori Sidek
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
Bharath904863
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
Ashim Lamichhane
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
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
TeshaleSiyum
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.pptPy-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
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
Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
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
sumitbardhan
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
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
TeshaleSiyum
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.pptPy-際際滷s-3 difficultpythoncoursefforbeginners.ppt
Py-際際滷s-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
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
Ad

Recently uploaded (20)

Comparative anatomy of brain of different types of vertebrates
Comparative anatomy of brain of different types of vertebratesComparative anatomy of brain of different types of vertebrates
Comparative anatomy of brain of different types of vertebrates
arpanmaji480
Dr. James Farley Identifies the Key Root Causes You Must Address to Heal Natu...
Dr. James Farley Identifies the Key Root Causes You Must Address to Heal Natu...Dr. James Farley Identifies the Key Root Causes You Must Address to Heal Natu...
Dr. James Farley Identifies the Key Root Causes You Must Address to Heal Natu...
DrJamesFarley
Aquatic ecosystem and its biomes involved.pdf
Aquatic ecosystem and its biomes involved.pdfAquatic ecosystem and its biomes involved.pdf
Aquatic ecosystem and its biomes involved.pdf
camillemaguid53
Pillbug Science Experiment 際際滷s Presentation
Pillbug Science Experiment 際際滷s PresentationPillbug Science Experiment 際際滷s Presentation
Pillbug Science Experiment 際際滷s Presentation
sawyerkwoodruff
International Journal of Pharmacological Sciences (IJPS)
International Journal of Pharmacological Sciences (IJPS)International Journal of Pharmacological Sciences (IJPS)
International Journal of Pharmacological Sciences (IJPS)
journalijps98
Metabolic acidosis and alkalosis ppt.pptx
Metabolic acidosis and alkalosis ppt.pptxMetabolic acidosis and alkalosis ppt.pptx
Metabolic acidosis and alkalosis ppt.pptx
aanchalm373
Entomopath angrau third year path 365pdf
Entomopath angrau third year path 365pdfEntomopath angrau third year path 365pdf
Entomopath angrau third year path 365pdf
zap6635
Abzymes mimickers in catalytic reactions at nanoscales
Abzymes mimickers in catalytic reactions at nanoscalesAbzymes mimickers in catalytic reactions at nanoscales
Abzymes mimickers in catalytic reactions at nanoscales
OrchideaMariaLecian
BOT-245_Breeding_of_Field_and_Horticultural_Crops.pdf
BOT-245_Breeding_of_Field_and_Horticultural_Crops.pdfBOT-245_Breeding_of_Field_and_Horticultural_Crops.pdf
BOT-245_Breeding_of_Field_and_Horticultural_Crops.pdf
zap6635
CULTIVATION - HARVESTING - PROCESSING - STORAGE -.pdf
CULTIVATION - HARVESTING - PROCESSING - STORAGE -.pdfCULTIVATION - HARVESTING - PROCESSING - STORAGE -.pdf
CULTIVATION - HARVESTING - PROCESSING - STORAGE -.pdf
Nistarini College, Purulia (W.B) India
BP_MXene_Project_Proposal_Presentation.pptx
BP_MXene_Project_Proposal_Presentation.pptxBP_MXene_Project_Proposal_Presentation.pptx
BP_MXene_Project_Proposal_Presentation.pptx
RoccoHunter8
Molecular biology and pellet weight marked
Molecular biology and pellet weight markedMolecular biology and pellet weight marked
Molecular biology and pellet weight marked
nandanitiwari82528
Introduction-to-Water-Pollution1234.pptx
Introduction-to-Water-Pollution1234.pptxIntroduction-to-Water-Pollution1234.pptx
Introduction-to-Water-Pollution1234.pptx
shilpakadf
Gene Expression & Regulation in Eukaryotes.pptx
Gene Expression & Regulation in Eukaryotes.pptxGene Expression & Regulation in Eukaryotes.pptx
Gene Expression & Regulation in Eukaryotes.pptx
Muhammad Hassan Asadi
Treatment of TMDS.by Dr.Devraj Neupane(MDS Resedent)
Treatment of TMDS.by Dr.Devraj Neupane(MDS Resedent)Treatment of TMDS.by Dr.Devraj Neupane(MDS Resedent)
Treatment of TMDS.by Dr.Devraj Neupane(MDS Resedent)
Dr.Devraj Neupane
esmo-academy-2024-pptx-template smyth final.pdf
esmo-academy-2024-pptx-template smyth final.pdfesmo-academy-2024-pptx-template smyth final.pdf
esmo-academy-2024-pptx-template smyth final.pdf
GuillermoGutirrez33
Kim Sandwich Biomes Project Science 10.pptx
Kim Sandwich Biomes Project Science 10.pptxKim Sandwich Biomes Project Science 10.pptx
Kim Sandwich Biomes Project Science 10.pptx
asantos691
the presentation on downstream processing (DSP).pptx
the presentation on downstream processing (DSP).pptxthe presentation on downstream processing (DSP).pptx
the presentation on downstream processing (DSP).pptx
aanchalm373
CloakingNote: A Novel Desktop Interface for Subtle Writing Using Decoy Texts
CloakingNote: A Novel Desktop Interface for Subtle Writing Using Decoy TextsCloakingNote: A Novel Desktop Interface for Subtle Writing Using Decoy Texts
CloakingNote: A Novel Desktop Interface for Subtle Writing Using Decoy Texts
sehilyi
Conservation Of Natural Resources 1.pptx
Conservation Of Natural Resources 1.pptxConservation Of Natural Resources 1.pptx
Conservation Of Natural Resources 1.pptx
nandhakumar2712013
Comparative anatomy of brain of different types of vertebrates
Comparative anatomy of brain of different types of vertebratesComparative anatomy of brain of different types of vertebrates
Comparative anatomy of brain of different types of vertebrates
arpanmaji480
Dr. James Farley Identifies the Key Root Causes You Must Address to Heal Natu...
Dr. James Farley Identifies the Key Root Causes You Must Address to Heal Natu...Dr. James Farley Identifies the Key Root Causes You Must Address to Heal Natu...
Dr. James Farley Identifies the Key Root Causes You Must Address to Heal Natu...
DrJamesFarley
Aquatic ecosystem and its biomes involved.pdf
Aquatic ecosystem and its biomes involved.pdfAquatic ecosystem and its biomes involved.pdf
Aquatic ecosystem and its biomes involved.pdf
camillemaguid53
Pillbug Science Experiment 際際滷s Presentation
Pillbug Science Experiment 際際滷s PresentationPillbug Science Experiment 際際滷s Presentation
Pillbug Science Experiment 際際滷s Presentation
sawyerkwoodruff
International Journal of Pharmacological Sciences (IJPS)
International Journal of Pharmacological Sciences (IJPS)International Journal of Pharmacological Sciences (IJPS)
International Journal of Pharmacological Sciences (IJPS)
journalijps98
Metabolic acidosis and alkalosis ppt.pptx
Metabolic acidosis and alkalosis ppt.pptxMetabolic acidosis and alkalosis ppt.pptx
Metabolic acidosis and alkalosis ppt.pptx
aanchalm373
Entomopath angrau third year path 365pdf
Entomopath angrau third year path 365pdfEntomopath angrau third year path 365pdf
Entomopath angrau third year path 365pdf
zap6635
Abzymes mimickers in catalytic reactions at nanoscales
Abzymes mimickers in catalytic reactions at nanoscalesAbzymes mimickers in catalytic reactions at nanoscales
Abzymes mimickers in catalytic reactions at nanoscales
OrchideaMariaLecian
BOT-245_Breeding_of_Field_and_Horticultural_Crops.pdf
BOT-245_Breeding_of_Field_and_Horticultural_Crops.pdfBOT-245_Breeding_of_Field_and_Horticultural_Crops.pdf
BOT-245_Breeding_of_Field_and_Horticultural_Crops.pdf
zap6635
BP_MXene_Project_Proposal_Presentation.pptx
BP_MXene_Project_Proposal_Presentation.pptxBP_MXene_Project_Proposal_Presentation.pptx
BP_MXene_Project_Proposal_Presentation.pptx
RoccoHunter8
Molecular biology and pellet weight marked
Molecular biology and pellet weight markedMolecular biology and pellet weight marked
Molecular biology and pellet weight marked
nandanitiwari82528
Introduction-to-Water-Pollution1234.pptx
Introduction-to-Water-Pollution1234.pptxIntroduction-to-Water-Pollution1234.pptx
Introduction-to-Water-Pollution1234.pptx
shilpakadf
Gene Expression & Regulation in Eukaryotes.pptx
Gene Expression & Regulation in Eukaryotes.pptxGene Expression & Regulation in Eukaryotes.pptx
Gene Expression & Regulation in Eukaryotes.pptx
Muhammad Hassan Asadi
Treatment of TMDS.by Dr.Devraj Neupane(MDS Resedent)
Treatment of TMDS.by Dr.Devraj Neupane(MDS Resedent)Treatment of TMDS.by Dr.Devraj Neupane(MDS Resedent)
Treatment of TMDS.by Dr.Devraj Neupane(MDS Resedent)
Dr.Devraj Neupane
esmo-academy-2024-pptx-template smyth final.pdf
esmo-academy-2024-pptx-template smyth final.pdfesmo-academy-2024-pptx-template smyth final.pdf
esmo-academy-2024-pptx-template smyth final.pdf
GuillermoGutirrez33
Kim Sandwich Biomes Project Science 10.pptx
Kim Sandwich Biomes Project Science 10.pptxKim Sandwich Biomes Project Science 10.pptx
Kim Sandwich Biomes Project Science 10.pptx
asantos691
the presentation on downstream processing (DSP).pptx
the presentation on downstream processing (DSP).pptxthe presentation on downstream processing (DSP).pptx
the presentation on downstream processing (DSP).pptx
aanchalm373
CloakingNote: A Novel Desktop Interface for Subtle Writing Using Decoy Texts
CloakingNote: A Novel Desktop Interface for Subtle Writing Using Decoy TextsCloakingNote: A Novel Desktop Interface for Subtle Writing Using Decoy Texts
CloakingNote: A Novel Desktop Interface for Subtle Writing Using Decoy Texts
sehilyi
Conservation Of Natural Resources 1.pptx
Conservation Of Natural Resources 1.pptxConservation Of Natural Resources 1.pptx
Conservation Of Natural Resources 1.pptx
nandhakumar2712013
Ad

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 functions name, data type of return value and of each formal parameter
  • 16. 16
  • 17. The Scope and Lifetime of a Variable A variables scope indicates where in the program it can be used Local or global Local variables are declared within a function or appear in a functions parameterList Global variables are declared outside of any function and remain in memory until program ends Avoid using global variables in your programs A variables 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