SlideShare 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 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

More Related Content

PPTX
Function in C Programming
DOCX
C programming language working with functions 1
PPTX
Parameter passing to_functions_in_c
PPTX
Function Parameters
PPTX
Function & Recursion
PPTX
PARAMETER PASSING MECHANISMS
PPTX
predefined and user defined functions
PDF
Python algorithm
Function in C Programming
C programming language working with functions 1
Parameter passing to_functions_in_c
Function Parameters
Function & Recursion
PARAMETER PASSING MECHANISMS
predefined and user defined functions
Python algorithm

What's hot (20)

PPTX
FUNCTION CPU
PPT
user defined function
PPTX
Functions
PPT
User Defined Functions in C
PPTX
Function in c++
PPTX
Functions in c
PDF
Functions and tasks in verilog
PPTX
User defined functions in C
PDF
Python recursion
PPT
Python Built-in Functions and Use cases
DOC
4. function
PDF
Notes: Verilog Part 5 - Tasks and Functions
PPT
Pre defined Functions in C
PDF
PPTX
Method parameters in c#
PPTX
functions of C++
PPTX
Inline function
PPTX
Functions in C - Programming
FUNCTION CPU
user defined function
Functions
User Defined Functions in C
Function in c++
Functions in c
Functions and tasks in verilog
User defined functions in C
Python recursion
Python Built-in Functions and Use cases
4. function
Notes: Verilog Part 5 - Tasks and Functions
Pre defined Functions in C
Method parameters in c#
functions of C++
Inline function
Functions in C - Programming
Ad

Similar to Function (20)

PDF
PSPC-UNIT-4.pdf
PPT
Functions
PPT
FUNCTIONS IN c++ PPT
PPTX
Functions
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPT
Chapter Introduction to Modular Programming.ppt
PPT
arrays.ppt
PPT
C++ CH4 in funtion using c++ in all ab.ppt
PPT
function_v1.ppt
PPT
function_v1.ppt
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PDF
Functions-Computer programming
PPT
function_v1fgdfdf5645ythyth6ythythgbg.ppt
DOCX
Introduction to c programming
DOCX
Functions assignment
PDF
Chapter 11 Function
PPT
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
PPTX
function of C.pptx
PPTX
Functions in C
PSPC-UNIT-4.pdf
Functions
FUNCTIONS IN c++ PPT
Functions
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Chapter Introduction to Modular Programming.ppt
arrays.ppt
C++ CH4 in funtion using c++ in all ab.ppt
function_v1.ppt
function_v1.ppt
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Functions-Computer programming
function_v1fgdfdf5645ythyth6ythythgbg.ppt
Introduction to c programming
Functions assignment
Chapter 11 Function
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
function of C.pptx
Functions in C
Ad

Recently uploaded (20)

PPTX
Cell Membrane: Structure, Composition & Functions
PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PDF
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
PDF
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
PDF
Sciences of Europe No 170 (2025)
PPT
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
PPTX
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PPTX
Introduction to Fisheries Biotechnology_Lesson 1.pptx
PPTX
2. Earth - The Living Planet Module 2ELS
PDF
AlphaEarth Foundations and the Satellite Embedding dataset
PPTX
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PPTX
Taita Taveta Laboratory Technician Workshop Presentation.pptx
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PDF
MIRIDeepImagingSurvey(MIDIS)oftheHubbleUltraDeepField
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
PDF
Phytochemical Investigation of Miliusa longipes.pdf
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
Cell Membrane: Structure, Composition & Functions
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
Sciences of Europe No 170 (2025)
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
TOTAL hIP ARTHROPLASTY Presentation.pptx
Introduction to Fisheries Biotechnology_Lesson 1.pptx
2. Earth - The Living Planet Module 2ELS
AlphaEarth Foundations and the Satellite Embedding dataset
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
ECG_Course_Presentation د.محمد صقران ppt
Taita Taveta Laboratory Technician Workshop Presentation.pptx
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
MIRIDeepImagingSurvey(MIDIS)oftheHubbleUltraDeepField
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
Phytochemical Investigation of Miliusa longipes.pdf
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.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