Professional Documents
Culture Documents
Lecture 8 - Function Part
Lecture 8 - Function Part
Lecture 8 - Function Part
Function (Part I)
Topics will be discussed :-
1. What is a function ?
2. C++ standard library function and
programmer defined function
3. Function declarations , definitions and call function
4. Passing argument and returning values
Input 2 nombor
Panggil Subprogram 1
Panggil Subprogram 2
Panggil Subprogram 3
Cetak semua jawapan
Input 2 Nombor
Panggil Subprogram 2
Cetak jawapan
Input 2 Nombor
Panggil Subprogram 3
Panggil Subprogram 1
Cetak jawapan
(Using Function)
# include <iostream>
# define pi 3.14 Example : Read radius of 4 circles. Calculate
using namespace std; and print the area of each circle in
function CALCULATE ( )
void CALCULATE ( );
int main ( )
{
int m;
for ( m =0; m<4;++m)
{
CALCULATE ( );
}
return 0;
}
void CALCULATE ( )
{
double r,a;
cin >> r;
a=pi*r*r;
cout <<“ Radius = “ << r <<“\tArea = “ << a;
cout <<endl;
}
Typical C++ program with function 3 important elements when
using function in any program
# include <header file>
1. Function Prototype
/* Declaration of Function Prototypes */ 2. Function Definition
void A (void ); 3. Call
(Placement of Function)
void main ( )
{
Local variable declaration for main ( )
A ( ) ; /* call function A */ Local variable ?
within function main ( ) / Driver All variables declared in
program function definition and are
} known only in the function
which they are defined.
/* Function Definitions */
void A (void ) Global variable ?
{ Declared outside any function
Local variable declaration for A ( ) and retain their value throughout
statements ; /* Operasi yang akan the execution of the program
dilaksanakan */
}
Write a driver program to get 2 positive integers A and B. Call a function to
compute and print C . C= A - B2
sqrt(B)
The order of the program execution is determined by the order of execution of the
function call statement within driver program
void KIRA ( int a, int b, double &va1, double &va2) => KIRA (a , b , v1 , v2 );
3. a. Write a prototype for a function called script that has three input argument ,
one char type , one int type and one double type. script will return a result
of type char.
b. Write call statement for function script
Perhaps the most valuable result of all education is the ability to make yourself do the thing you
have to do ,when it ought to be done , whether you like it or not. This is the first lesson to be learned.
- Thomas Huxley -
Function with input parameter
This type of function will receive input(s) and can return / not return a single value.
Input Output
Function
Input1 Output
Function
Input2
Input
Function
Input1
Function
Input2
Contoh 1
A= s ( s a )( s b)( s c)
where s is the semiperimeter of the triangle. Write a driver program to get values for
a , b and c and call your function to compute A. The driver should print A , a , b , and
c
a c
s = (a+b+c)
2
b
Jejari bulatan : 4 m
Luas bulatan : 50.24 m2
Perimeter bulatan : 25.12 m
Semua fungsi dipanggil dari driver program yang sama iaitu main( )
Jejari (meter) adalah input data yang dimasukkan melalui driver program
#include <iostream> double LILITAN (double j)
#include <iomanip> {
#define pi 3.14 return (2 * pi * j );
}
double LUAS (double j);
double LILITAN (double j);
void CETAK(double Luas , double Perimeter ); void CETAK(double Luas , double PR,
double j )
int main () {
{ cout <<“Jejari Bulatan = “ << j <<“ m”;
double r,A,P; cout <<“\nLuas Bulatan = “ <<Luas <<“ m2” ;
cin >> r; cout <<“\nPerimeter Bulatan = “ <<PR<<“ m”;
A=LUAS ( r ); }
P=LILITAN ( r );
CETAK (A , P , r );
return 0;
} Input
4
double LUAS (double j) Output
{ Jejari Bulatan = 4 m
return ( pi * j * j ); Luas Bulatan = 50.24 m2
} Perimeter Bulatan = 25.12 m
Example 2
1. Write a function that returns the smallest of three floating point numbers.
The program should prompt the user whether they want to try another 3
set of floating point number. If yes , call the function , repeat until the
user’s response is no
Example Output
Number 1 : 25.87
Number 2 : 390.56
Number 3 : 19.50
……..etc…….
Solution ex 2
double smallest (double a,double b,double c)
# include <iostream>
# include <iomanip> {
using namespace std; double s;
double smallest (double a,double b,double c); if (a<b)
s=a;
int main () else
{ s=b;
double A,B,C,SMALL; if (c<s)
char ans='Y';
s=c;
while (ans == 'Y' || ans == 'y')
{ return s;
cout <<"\n\nEnter 3 Floating Point Numbers : "; }
cin >> A>>B>>C;
SMALL = smallest(A,B,C);
cout <<"\nNumber 1 : "<<setprecision(4)<<A;
cout <<"\nNumber 2 : "<<setprecision(4)<<B;
cout <<"\nNumber 3 : "<<setprecision(4)<<C;
cout <<"\n\nSmallest Number is : "<<SMALL;
cout <<"\n\nTry Another Set ? Press Y to continue”
cout <<“\nPress any key to exit...: " ;
cin >> ans;
}
return 0;
}
Call by value
- A copy of argument is made and passed to the called function
- Changes to the copy do not effect the original variable’s value in the caller (driver
program)
- e.g. kira(x,y)
Call by reference
- passing addresses
- The caller (driver program) gives the called function the ability to access the
caller’s data directly and to modify the data
- e.g. swap(&x,&y );
Contoh 3
Buat satu aturcara yang boleh mendapatkan kuasa dua , kuasa tiga dan kuasa empat bagi
satu nombor (double ) yang dibaca melalui driver program – main ( ) . Bina satu fungsi
yang akan mendapatkan kuasa 2,3,dan 4 bagi nombor tersebut. Semua cetakan output
hendaklah buat di dalam main ( )
Input
4.5
Output
Kuasadua bagi 4 ialah 20.25
Kuasatiga bagi 4 ialah 91.125
Kuasaempat bagi 4 ialah 410.063
Penyelesaian #include <iostream>
Contoh 3 #include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
double Nombor ,K2,K3,K4;
cin >> Nombor;
Proses ( Nombor , K2 , K3 , K4 );
cout <<“Kuasadua bagi “ <<Nombor <<“ ialah “ << K2;
cout <<“\nKuasatiga bagi “ <<Nombor <<“ ialah “ << K3;
cout <<“\nKuasaempat bagi “ <<Nombor <<“ ialah “ << K4;
return 0;
}
#include <iostream>
#include <iomanip> Output
int main ()
{
int x , y;
x = 5 ; y = 10;
cout <<"x = "<<x<<"\t"<<"y = "<<y <<endl;
swap(x,y );
cout <<"x = "<<x<<"\t"<<"y = "<<y <<endl;
return 0;
}
void swap (int &a, int &b )
{
int temp = 0 ;
temp = a ;
a=b;
b = temp ;
}
Tugasan 3
How much is 6 x 7 ?
The student then types the answer. Your function checks the student’s answer.
If it is correct , print “Very Good” , if the answer is wrong , print “No ,
Please try again ” and let the student try the same question again repeatedly
until the student finally gets its right.
From the driver program , prompt the student if he would like to try another
question. If yes , ask another multiplication question and if not , exit the
program.
b) Write a program for an Automatic Teller Machine that dispenses money.
The user should enter the amount desired and the machine dispenses this
amount using the least number of bills. The bills dispensed are 50s ,10s
and 1s. Write a function that will determine how many each kind of
bill to dispense.
Write a driver program to get amount desired. Your driver program also
should print the output (the number of each bill to dispense.)