Académique Documents
Professionnel Documents
Culture Documents
FUNCTION
What is FUNCTION???
Afunctionis a group of statements that
separatefunctions.
FUNCTION
A function may return
a value. The
return_typeis the
data type of the value
the function returns.
A parameter is like a
placeholder. When a
function is invoked, you
pass a value to the
parameter.
data type of the value the function returns. Some functions perform
the desired operations without returning a value. In this case, the
return_type is the keywordvoid.
Function Name:This is the actual name of the function. The
function name and the parameter list together form the function
signature.
Parameters:A parameter is like a placeholder. When a function is
EXAMPLE
Example 1:
#include <iostream>
using namespace std;
//preprocessor
Main function
Function
Predefined/built in
function
Main Function
A main function is automatically execute first
another function.
Predefined/Built-in
Function
You already experience some predefined
Example
//preprocessor
#include<iostream>
#include<math>
using namespace std;
void main() //main
function
{
cout<<X;
cout<<sqrt(9);
}
Return answer=3
int sqrt()
{
------------------return ()
}
Call sqrt in
math.h
Send=9
Accept=9
i) Function Prototype
<return data type><function
name>(<parameter list>);
The compiler refers to the function prototype to
check that calls to a function contain:
a. The correct return type
b. The correct number of arguments
c. The correct argument types
d. The arguments are in the correct order
Return
Accep
t
//function header
int kuasa3(int x)
{
//local variable&constant
int z;
Function
z=x*x*x;
body
return (z);
}
Example 1
//preprocessor
#include<iostream>
using namespace std;
//function prototype
int kuasa3(int x);
//main function
void main()
{
int jwp, x;
1. Calling function
cin >>x;
jwp=kuasa3(x); kuasa3
send= x
cout<<jwp;
}
3.
Return
Function
body
Example 2
//preprocessor
#include<iostream>
using namespace std;
//function prototype
int isipadu(int x, int y, int z);
-Calling function isipadu
//main function
-send x, y, z
void main()
-must send 3 argument
{
-must be int
int jwp, x, y, z;
-Must in correct order as
cin >>x>>y>>z;
declare in function prototype
jwp=isipadu(x,y,z);
cout<<jwp;
}
3.
Return
2. Accept
int isipadu(int x, int y, int z)
x,y ,z
{
int isi; //local variable
Function
isi=x*y*z;
body
return (isi);
}
Common error!!
A function cannot be defined inside another
function
Int isipadu (int x, y, z) is WRONG!
Int isipadu(int x; int y; int z) is WRONG!
POP QUIZ!!
Write a complete program using function to
//preprocessor
#include<iostream>
using namespace std;
//function prototype
float circle(int r);
//main function
void main()
{
float circleArea;
int r;
cin >>r;
circleArea=circle(r);
cout<<circleArea;
}
float circle (int r)
{
float area; //local variable
area=3.14 * r*r;
return (area);
}
AN
S
R
W
Example
//function definition
//function header
void Display()
{
cout<<name \t street\t Address\t\t state\t city\t
postcode\n;
cout<<_____ \t____\t_____\t_____\t______\t______\t;
}
OR
void Display()
{
cout<<name \t street\t Address\t\t state\t city\t
postcode\n;
cout<<_____ \t____\t_____\t_____\t______\t______\t;
return(0);
}
X is actual argument
y is formal argument
example
//preprocessor
#include<iostream>
using namespace std;
//function prototype
double purata(float x, float y,
float z);
//function definition
//function header
double purata(float a, float b,
float c)
{
double jawapan;
jawapan=(a+b+c)/3;
return(jawapan);
}
//main function
void main()
{ float x, y, z, jawapan;
cout<<sila masukkan markah
1:;
Actual argument
cin>>x;
cout<<sila masukkan markah
2:;
cin>>y;
cout<<sila masukkan markah
3:;
cin>>z;
jawapan=purata(x,y,z);
cout<<purata markah untuk 3
subjek:;
formal argument
void main()
{
//define actual argument variable
int a=0;
int b=0;
Example
//calling function
passByValue(a,b);
//display actual argument value
cout<<actual argument, a=<<a;
cout<<actual argument b=<<b;
}
void passByValue(int x, int y)
{
++x;
--y;
//display formal argument value
cout<<formal argument, x=<<x;
cout<<\nformal argument, y=<<y;
}
output
Formal Argument, x=1
Formal Argument y=-1
Actual Argument a=0
Actual Argument b=0
Reference parameter
A reference parameter provides two way
examples
void main()
{
//define actual argument variable
int a=0;
int b=0;
//calling function
passByReference(a,b);
//display actual argument value
cout<<actual argument, a=<<a;
cout<<actual argument b=<<b;
}
void passByReference(int &x, int &y)
{
++x;
--y;
//display formal argument value
cout<<formal argument, x=<<x;
cout<<\nformal argmument, y=<<y;
}
output
Formal Argument, x=1
Formal Argument y=-1
Actual Argument a=1
Actual Argument b=-1
EXAMPLE
#include<iostream>
using namespace std;
//function prototype/declaration
void funct_ref(int &z1, int z2);
int main()
{
int x = 1;
int y = 1;
cout<<"x is "<<x<<endl;
cout<<"y is "<<y<<endl;
}
//function definition
void funct_ref(int &z1, int
z2)
{
++z1;
++z2;
cout<<"x is "<<x<<endl;
cout<<"y is "<<y<<endl;
}
RECURSION
Recursion is a process whereby an operation/function
LOOPING STATEMENT
for(int i=0; i<10; i++)
{
cout << "The number is: " << i <<
endl;
}
The
The
The
The
The
The
The
The
The
The
number
number
number
number
number
number
number
number
number
number
is:
is:
is:
is:
is:
is:
is:
is:
is:
is:
0
1
2
3
4
5
6
7
8
9
EXAMPLE 1- RECURSION
#include <iostream>
using namespace std;
void numberFunction(int i)
{
cout << "The number is: " << i <<
endl;
i++;
if(i<10)
{
numberFunction(i);
}
}
int main()
{
int i = 0;
numberFunction(i);
return 0;
EXAMPLE 1- RECURSION
#include <iostream>
using namespace std;
void numberFunction(int i) {
cout << "The number is: " << i <<
endl;
i++;
if(i<10) {
numberFunction(i);
}
}
int main() {
int i = 0;
numberFunction(i);
return 0;
}
EXAMPLE 2 RECURSION
#include <iostream>
using namespace std;
int factorial(int);
int main() {
int n;
cout<<"Enter a number to find factorial: ";
cin>>n;
cout<<"Factorial of "<<n<<" =
"<<factorial(n);
return 0; }
int factorial(int n) {
if (n>1) {
return n*factorial(n-1);
}
else {
return 1;
}
EXPLANATION
Sekian,
Terima kasih atas perhatian yang
TERBAIKK!!