SlideShare a Scribd company logo
1 of 48
FUNCTIONFUNCTION
1
INTRODUCTION
 To make large program manageable,
programmers modularize them into
subprograms.
 These subprograms are called functions
 They can be compiled and tested separately
and being reuse in different programs.
 This modularization is one of the characteristics
of successful object-oriented software.
2
BENEFIT OF USING FUNCTION
 The main program is simplified where it will
contain the function call statement. By doing
that planning, coding, testing, debugging,
understanding and maintaining a computer
program will be easier.
 The same function can be reused in another
program, which will prevent code duplication and
reduce the time of writing the program.
3
BUILT-IN FUNCTION
 Built-in function or predefined function are function
used by the programmers in order to speed up
program writing.
 Programmer may use the existing code to perform
common task without having to rewrite any code.
 These functions are predefined by the producer of
compiler , (c++) and are stored in the header file (.h
files) called libraries.
 In order to for program to use pre-defined function,
the appropriate library file must be included in the
program using the #include directive
4
SYNTAX FORM
#include<header file name>
Example:
To use the getline(), program must include the
directive
#include<string.h>
5
#include < filename>
#include < iostream.h>
#include < math.h>
#include < string.h>
{
:::::::::::
:::::::::::
}
6
iostream.h
The name iostream.h stands for
input/output stream header file. The basic I/O
objects contained in this header files are cin
for console input (the keyboard) and cout for
console output (the screen). cin operator is
used with the operator>> while cout is used
with operator <<. 7
#include<iostream>
using namespace std;
int main()
{
char a;
cout<<“Insert your favaourite character:";
cin.get(a);
cout<<"The character is:“;
cout.put(a);
return 0;
}
8
COMMONLY USED FUNCTION
Name Description
get() for console input(keyboard) of
type char
put() For console output (screen) of
type char
9
iomanip.h
The iomanip header file contains
function for formatting your output. For
example, the function setw (n) prints your
output in n columns.
10
ctype.h
This file contains function such as for
determining whether a given character is
alphanumeric, alphabetic, digital,
uppercase or lowercase.
11
 Refer hard copy
12
USER DEFINED FUNCTION
 Programmer are able to create their own function
since the built-in function in c++ libraries are
limited to perform other programming task.
 User define function are function whose task are
determined by the programmer and defined
within the program in which the function is used.
13
IMPORTANT COMPONENT OF
FUNCTION:
 In order to use user define function, a
programmer must satisfy three requirements:
 Function prototype
 Function call
 Function definition
14
FUNCTION PROTOTYPE
DECLARATIONS
 All function must be declared before they can
used in a program.
 Placed just before the mian program and
sometimes at beginning of the mian program.
 A function prototype sepecifies:
 The name of function
 The order and type of parameters
 The return value.
15
FUNCTION PROTOTYPEFUNCTION PROTOTYPE
return_type function_name (type parameter_list);
Example:
void sum ();
int findsum(int,int);
float average(int,int,float,float);
16
FUNCTION DEFINITION
 A function must be defined before it can carry out
the task assigned to it.
 In other word the statement that perform the
task are inside the function definition.
 A function is written once in the program and
can be used by any other function in the
program.
 The function definition is placed either before the
main() or after the main().
17
BEFORE THE MAIN()
The function prototype is not required.
int sum()
{
statement;
}
int main()
{
sum();
} 18
AFTER THE MAIN()
int sum() //function prototype
int main()
{
sum();
}
int sum();
{
statement;
}
19
 Component of function definition:
• Function header
• Function body
• Local variable
• Return statement
• Parameters declaration list
20
return_type function_name(parameter list)
{
Variable declaration
……
return expression
}
21
•return_type is any valid C++ data type
(int,float,char)
•Function_name is any valid C++ identifier
•Parameter_list is a list of parameters separated
by commas
•Variable declaration is a list of variables
declared within the function
return is a C++ keyword
•Expression is the value returned by the function
Function
header
Function
body
FUNCTION HEADERSyntax:
return_type function_name(parameter list)
The function header defines the return type,
function name and list of parameters.
It is writen the same way as the function
prototype, except that the header does not end with
semicolon.
Function header contains the following:
i. Type of data , if any, is returned from the
function when operation has completed.
ii. The name of function
iii. Type of data , if any, is sent into the
function.
22
FUNCTION BODY
 The function body contain declaration of local
variable and executable statements needed to
perform a task assigned to the function.
23
EXAMPLE:
void message( )
{
cout<<“Hello!!”;
}
24
EXAMPLE:
int sum3num(int a,int b,int c)
{
int total=a+b+c;
return total;
}
25
EXPLANATION:
 The keyword int in the function header
indicated that the function will return an
integer value.
 The parameter list int a, int b and int c
indicated that three integer values will be
sent into and used in the function sum3num.
 Return total indicated that the value of total
will be returned to the calling function.
26
PARAMETER
 There are two types of parameters:
 Formal parameter
 Actual parameter
27
FORMAL PARAMETER
 Formal parameter is arguments in the header of
function definition.
 Formal parameter contains the value that being
passed by actual parameters
passed from the function call.
28
EXAMPLE:
 The following function takes three formal
parameters to calculate the sum.
void calSum(int num1, int num2, int num3)
{
cout<<“Total:”<<num1+num2+num3;
}
29
ACTUAL PARAMETER
 Actual parameter is a constant , variable or
expression in a function call that corresponds to
the actual parameter.
 Actual parameters contain values that will be
passed to the function definition.
30
EXAMPLE:
 The following function contains three values which will
be passed to the calSum().
void main()
{
calSum(10,9,11)
}
void calSum(int num1, int num2, int num3)
{
cout<<“Total:”<<num1+num2+num3;
}
31
Actual parameter
Formal
Parameter
FUNCTION CALL
 To use the function written, you have to call it
back in function main().
 When a function is called, the function body is
transferred to the called function (function
definition).
 Once the function is called, the task will be
executed.
32
SYNTAX FOR FUNCTION CALL
 function_name(parameterArgument)
33
#include<iostream>
using namespace std;
int main()
{
message()
}
void message( )
{
cout<<“Hello!!”;
}
34
#include <iostream.h>
Void sum(float,float,float);
 
void main ( )
{
float x,y,z;
cout <<"enter three numbers:n" ;
cin >>x>>y>>z;
sum(x,y,z);
}
void sum(float x, float y, float z)
{
cout <<"n Sum = x + y + z = "<< x+y+z;
} 35
IQ TEST
Identify VALID or INVALID for declaration of
function below:
1. float average(float x, float y);
2. int cube(int i);
3. Square(int x1, x2, float yl)
4. Char convert(char ch, int a,b);
36
PROGRAM SCOPEPROGRAM SCOPE
 The scope of an identifier is that part of the
program where it can be used. For example
variable cannot be used before they are
declared, so their scopes begin where they are
declared.
37
FUNCTION SCOPEFUNCTION SCOPE
 Function scope is the scope of a name consist
of that part of the program where it can be used.
It begins where the name is declared. If that
declaration is inside a function (including the
main() function), then the scope extends to the
end of the innermost block that contains the
declaration
38
EXAMPLE:
39
void f()
void g()
int x = 11;
main ()
{
int x = 22;
{
int x = 33;
cout << "In block inside main(): x= " << x<<endl;
}
cout << " In main() : x = " <<x<<endl;
cout << " In main (): ::x =" << ::x<<endl;
f();
g();
}
Global scope
Global variable
Local scope
LOCAL VARIABLELOCAL VARIABLE Variable that is declared inside a block. It is
accessible only from within that block.
40
Global VariableGlobal Variable Variables defined outside of any function have global scope and thus are available from any function in the
program, including main().
 Global variable is not declared within a function. Any function defined after the declaration can access that
variable as long as the function does not declare its own variable with that name
41
#include <iostream.h>
float Convert(float);
int main()
{
float TempFer;
float TempCel;
cout << "Please enter the temperature in Fahrenheit: ";
cin >> TempFer;
TempCel = Convert(TempFer);
cout << "nHere's the temperature in Celsius: ";
cout << TempCel << endl;
return 0;
}
float Convert(float TempFer)
{
float TempCel;
TempCel = ((TempFer - 32) * 5) / 9;
return TempCel;
}
LL
oo
cc
aa
ll
vv
aa
rr
ii
aa
bb
ll
ee
ss
42
GG
ll
oo
bb
aa
ll
vv
aa
rr
ii
aa
bb
ll
ee
ss
#include <iostream.h>
void myFunction(); // prototype
int x = 5, y = 7; // global variables
int main()
{
cout << "x from main: " << x << "n";
cout << "y from main: " << y << "nn";
myFunction();
cout << "Back from myFunction!nn";
cout << "x from main: " << x << "n";
cout << "y from main: " << y << "n";
return 0;
}
void myFunction()
{
int y = 10;
cout << "x from myFunction: " << x << "n";
cout << "y from myFunction: " << y << "nn";
}
  
MAKING FUNCTION CALLMAKING FUNCTION CALL
 A function call is an expression that can be
used as a single statement or within other
statement
43
Start
Function
Call
Function
Execution
Function
Return
End
Program Execution Flow
ARGUMENT PASSING BY VALUEARGUMENT PASSING BY VALUE
 Expression used in the function call is
evaluated first and then the resulting value is
assigned to the corresponding parameter in
the function parameter list before the function
begins executing.
 If y has value 9, then the value 9 is passed to the
local variable y before the function begins to
execute its statements.
44
PROGRAM
#include <iostream.h>
void sum(int,int);
void main ( )
{
int x,y;
cout <<"enter two numbers:n" ;
cin >>x>>y;
 
sum(x,y);
}
 
void sum(int x, int y)
{
cout <<"n Sum = x + y = "<< x+y;
}
45
ARGUMENT PASSING BYARGUMENT PASSING BY
REFERENCEREFERENCE
 To pass parameter by reference instead of by
value, simply append an ampersand & to the
type specifies in the function parameter list.
This makes the local variable a reference to
the actual parameter passed to it.
46
RETURN (VOID)RETURN (VOID)
 Functions return a value or return void. Void is a signal to the
compiler that no value will be returned.
 return 3;
 return (y > 3);
 return (Function1());  
These are all legal return statements, assuming that the function
Function1( ) itself returns a value. The value in the second
statement, return (y > 3), will be zero if y is not greater than 3 or it
will be 1. What is returned is the value of the expression, 0 (false)
or 1 (true), not the value of x.
47
RETURN (VOID)RETURN (VOID)
 To return a value from a function, write the keyword
return followed by the value you want to return. The
value might itself be an expression that returns a value.
48

More Related Content

What's hot (20)

Functions in C
Functions in CFunctions in C
Functions in C
 
Function in C
Function in CFunction in C
Function in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Function in c
Function in cFunction in c
Function in c
 
4. function
4. function4. function
4. function
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
user defined function
user defined functionuser defined function
user defined function
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
The Ring programming language version 1.9 book - Part 28 of 210
The Ring programming language version 1.9 book - Part 28 of 210The Ring programming language version 1.9 book - Part 28 of 210
The Ring programming language version 1.9 book - Part 28 of 210
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 

Similar to POLITEKNIK MALAYSIA

CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfJAVVAJI VENKATA RAO
 
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].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functionsnikshaikh786
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurThakurkirtika
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdfsantosh147365
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfBoomBoomers
 

Similar to POLITEKNIK MALAYSIA (20)

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
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Functions
Functions Functions
Functions
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
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
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakur
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Basic c++
Basic c++Basic c++
Basic c++
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdf
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
C structure
C structureC structure
C structure
 

More from Aiman Hud

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 

More from Aiman Hud (20)

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

Recently uploaded

JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfJORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfArturo Pacheco Alvarez
 
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Eticketing.co
 
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxFrance's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxEuro Cup 2024 Tickets
 
Technical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeTechnical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeOptics-Trade
 
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样7pn7zv3i
 
Technical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeTechnical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeOptics-Trade
 
Technical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeTechnical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeOptics-Trade
 
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Judith Chuquipul
 
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/78377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7dollysharma2066
 
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝soniya singh
 
Expert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLExpert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLAll American Billiards
 
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best ServicesMysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Servicesnajka9823
 
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdfJORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdfArturo Pacheco Alvarez
 
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyReal Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyApk Toly
 
IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.SJU Quizzers
 
ppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interestppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my InterestNagaissenValaydum
 
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeInstruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeOptics-Trade
 
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai EbonyDubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebonyhf8803863
 

Recently uploaded (20)

JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfJORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
 
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Serviceyoung Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
 
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
 
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxFrance's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
 
Technical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeTechnical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics Trade
 
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
 
FULL ENJOY Call Girls In Savitri Nagar (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In  Savitri Nagar (Delhi) Call Us 9953056974FULL ENJOY Call Girls In  Savitri Nagar (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In Savitri Nagar (Delhi) Call Us 9953056974
 
Technical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeTechnical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics Trade
 
Technical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeTechnical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics Trade
 
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
 
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/78377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
 
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
 
Expert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLExpert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FL
 
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best ServicesMysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
 
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdfJORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
 
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyReal Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
 
IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.
 
ppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interestppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interest
 
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeInstruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
 
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai EbonyDubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
 

POLITEKNIK MALAYSIA

  • 2. INTRODUCTION  To make large program manageable, programmers modularize them into subprograms.  These subprograms are called functions  They can be compiled and tested separately and being reuse in different programs.  This modularization is one of the characteristics of successful object-oriented software. 2
  • 3. BENEFIT OF USING FUNCTION  The main program is simplified where it will contain the function call statement. By doing that planning, coding, testing, debugging, understanding and maintaining a computer program will be easier.  The same function can be reused in another program, which will prevent code duplication and reduce the time of writing the program. 3
  • 4. BUILT-IN FUNCTION  Built-in function or predefined function are function used by the programmers in order to speed up program writing.  Programmer may use the existing code to perform common task without having to rewrite any code.  These functions are predefined by the producer of compiler , (c++) and are stored in the header file (.h files) called libraries.  In order to for program to use pre-defined function, the appropriate library file must be included in the program using the #include directive 4
  • 5. SYNTAX FORM #include<header file name> Example: To use the getline(), program must include the directive #include<string.h> 5
  • 6. #include < filename> #include < iostream.h> #include < math.h> #include < string.h> { ::::::::::: ::::::::::: } 6
  • 7. iostream.h The name iostream.h stands for input/output stream header file. The basic I/O objects contained in this header files are cin for console input (the keyboard) and cout for console output (the screen). cin operator is used with the operator>> while cout is used with operator <<. 7
  • 8. #include<iostream> using namespace std; int main() { char a; cout<<“Insert your favaourite character:"; cin.get(a); cout<<"The character is:“; cout.put(a); return 0; } 8
  • 9. COMMONLY USED FUNCTION Name Description get() for console input(keyboard) of type char put() For console output (screen) of type char 9
  • 10. iomanip.h The iomanip header file contains function for formatting your output. For example, the function setw (n) prints your output in n columns. 10
  • 11. ctype.h This file contains function such as for determining whether a given character is alphanumeric, alphabetic, digital, uppercase or lowercase. 11
  • 12.  Refer hard copy 12
  • 13. USER DEFINED FUNCTION  Programmer are able to create their own function since the built-in function in c++ libraries are limited to perform other programming task.  User define function are function whose task are determined by the programmer and defined within the program in which the function is used. 13
  • 14. IMPORTANT COMPONENT OF FUNCTION:  In order to use user define function, a programmer must satisfy three requirements:  Function prototype  Function call  Function definition 14
  • 15. FUNCTION PROTOTYPE DECLARATIONS  All function must be declared before they can used in a program.  Placed just before the mian program and sometimes at beginning of the mian program.  A function prototype sepecifies:  The name of function  The order and type of parameters  The return value. 15
  • 16. FUNCTION PROTOTYPEFUNCTION PROTOTYPE return_type function_name (type parameter_list); Example: void sum (); int findsum(int,int); float average(int,int,float,float); 16
  • 17. FUNCTION DEFINITION  A function must be defined before it can carry out the task assigned to it.  In other word the statement that perform the task are inside the function definition.  A function is written once in the program and can be used by any other function in the program.  The function definition is placed either before the main() or after the main(). 17
  • 18. BEFORE THE MAIN() The function prototype is not required. int sum() { statement; } int main() { sum(); } 18
  • 19. AFTER THE MAIN() int sum() //function prototype int main() { sum(); } int sum(); { statement; } 19
  • 20.  Component of function definition: • Function header • Function body • Local variable • Return statement • Parameters declaration list 20
  • 21. return_type function_name(parameter list) { Variable declaration …… return expression } 21 •return_type is any valid C++ data type (int,float,char) •Function_name is any valid C++ identifier •Parameter_list is a list of parameters separated by commas •Variable declaration is a list of variables declared within the function return is a C++ keyword •Expression is the value returned by the function Function header Function body
  • 22. FUNCTION HEADERSyntax: return_type function_name(parameter list) The function header defines the return type, function name and list of parameters. It is writen the same way as the function prototype, except that the header does not end with semicolon. Function header contains the following: i. Type of data , if any, is returned from the function when operation has completed. ii. The name of function iii. Type of data , if any, is sent into the function. 22
  • 23. FUNCTION BODY  The function body contain declaration of local variable and executable statements needed to perform a task assigned to the function. 23
  • 25. EXAMPLE: int sum3num(int a,int b,int c) { int total=a+b+c; return total; } 25
  • 26. EXPLANATION:  The keyword int in the function header indicated that the function will return an integer value.  The parameter list int a, int b and int c indicated that three integer values will be sent into and used in the function sum3num.  Return total indicated that the value of total will be returned to the calling function. 26
  • 27. PARAMETER  There are two types of parameters:  Formal parameter  Actual parameter 27
  • 28. FORMAL PARAMETER  Formal parameter is arguments in the header of function definition.  Formal parameter contains the value that being passed by actual parameters passed from the function call. 28
  • 29. EXAMPLE:  The following function takes three formal parameters to calculate the sum. void calSum(int num1, int num2, int num3) { cout<<“Total:”<<num1+num2+num3; } 29
  • 30. ACTUAL PARAMETER  Actual parameter is a constant , variable or expression in a function call that corresponds to the actual parameter.  Actual parameters contain values that will be passed to the function definition. 30
  • 31. EXAMPLE:  The following function contains three values which will be passed to the calSum(). void main() { calSum(10,9,11) } void calSum(int num1, int num2, int num3) { cout<<“Total:”<<num1+num2+num3; } 31 Actual parameter Formal Parameter
  • 32. FUNCTION CALL  To use the function written, you have to call it back in function main().  When a function is called, the function body is transferred to the called function (function definition).  Once the function is called, the task will be executed. 32
  • 33. SYNTAX FOR FUNCTION CALL  function_name(parameterArgument) 33
  • 34. #include<iostream> using namespace std; int main() { message() } void message( ) { cout<<“Hello!!”; } 34
  • 35. #include <iostream.h> Void sum(float,float,float);   void main ( ) { float x,y,z; cout <<"enter three numbers:n" ; cin >>x>>y>>z; sum(x,y,z); } void sum(float x, float y, float z) { cout <<"n Sum = x + y + z = "<< x+y+z; } 35
  • 36. IQ TEST Identify VALID or INVALID for declaration of function below: 1. float average(float x, float y); 2. int cube(int i); 3. Square(int x1, x2, float yl) 4. Char convert(char ch, int a,b); 36
  • 37. PROGRAM SCOPEPROGRAM SCOPE  The scope of an identifier is that part of the program where it can be used. For example variable cannot be used before they are declared, so their scopes begin where they are declared. 37
  • 38. FUNCTION SCOPEFUNCTION SCOPE  Function scope is the scope of a name consist of that part of the program where it can be used. It begins where the name is declared. If that declaration is inside a function (including the main() function), then the scope extends to the end of the innermost block that contains the declaration 38
  • 39. EXAMPLE: 39 void f() void g() int x = 11; main () { int x = 22; { int x = 33; cout << "In block inside main(): x= " << x<<endl; } cout << " In main() : x = " <<x<<endl; cout << " In main (): ::x =" << ::x<<endl; f(); g(); } Global scope Global variable Local scope
  • 40. LOCAL VARIABLELOCAL VARIABLE Variable that is declared inside a block. It is accessible only from within that block. 40 Global VariableGlobal Variable Variables defined outside of any function have global scope and thus are available from any function in the program, including main().  Global variable is not declared within a function. Any function defined after the declaration can access that variable as long as the function does not declare its own variable with that name
  • 41. 41 #include <iostream.h> float Convert(float); int main() { float TempFer; float TempCel; cout << "Please enter the temperature in Fahrenheit: "; cin >> TempFer; TempCel = Convert(TempFer); cout << "nHere's the temperature in Celsius: "; cout << TempCel << endl; return 0; } float Convert(float TempFer) { float TempCel; TempCel = ((TempFer - 32) * 5) / 9; return TempCel; } LL oo cc aa ll vv aa rr ii aa bb ll ee ss
  • 42. 42 GG ll oo bb aa ll vv aa rr ii aa bb ll ee ss #include <iostream.h> void myFunction(); // prototype int x = 5, y = 7; // global variables int main() { cout << "x from main: " << x << "n"; cout << "y from main: " << y << "nn"; myFunction(); cout << "Back from myFunction!nn"; cout << "x from main: " << x << "n"; cout << "y from main: " << y << "n"; return 0; } void myFunction() { int y = 10; cout << "x from myFunction: " << x << "n"; cout << "y from myFunction: " << y << "nn"; }
  • 43.    MAKING FUNCTION CALLMAKING FUNCTION CALL  A function call is an expression that can be used as a single statement or within other statement 43 Start Function Call Function Execution Function Return End Program Execution Flow
  • 44. ARGUMENT PASSING BY VALUEARGUMENT PASSING BY VALUE  Expression used in the function call is evaluated first and then the resulting value is assigned to the corresponding parameter in the function parameter list before the function begins executing.  If y has value 9, then the value 9 is passed to the local variable y before the function begins to execute its statements. 44
  • 45. PROGRAM #include <iostream.h> void sum(int,int); void main ( ) { int x,y; cout <<"enter two numbers:n" ; cin >>x>>y;   sum(x,y); }   void sum(int x, int y) { cout <<"n Sum = x + y = "<< x+y; } 45
  • 46. ARGUMENT PASSING BYARGUMENT PASSING BY REFERENCEREFERENCE  To pass parameter by reference instead of by value, simply append an ampersand & to the type specifies in the function parameter list. This makes the local variable a reference to the actual parameter passed to it. 46
  • 47. RETURN (VOID)RETURN (VOID)  Functions return a value or return void. Void is a signal to the compiler that no value will be returned.  return 3;  return (y > 3);  return (Function1());   These are all legal return statements, assuming that the function Function1( ) itself returns a value. The value in the second statement, return (y > 3), will be zero if y is not greater than 3 or it will be 1. What is returned is the value of the expression, 0 (false) or 1 (true), not the value of x. 47
  • 48. RETURN (VOID)RETURN (VOID)  To return a value from a function, write the keyword return followed by the value you want to return. The value might itself be an expression that returns a value. 48