Vous êtes sur la page 1sur 36

CHAPTER 5

FUNCTION

What is FUNCTION???
Afunctionis a group of statements that

together perform a task. C++ programs are


simply a collection of functions.
EveryC++program has at least

onefunction, which is main(), and some


programs can defineadditionalfunctions.
You can divide up your code into

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.

return_type function_name( parameter


list )
{
body of the function
}

Return Type: A function may return a value. Thereturn_typeis the

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

invoked, you pass a value to the parameter. This value is referred to


as actual parameter or argument. The parameter list refers to the
type, order, and number of the parameters of a function. Parameters
are optional; that is, a function may contain no parameters.
Function Body:The function body contains a collection of

statements that define what the function does.

EXAMPLE
Example 1:
#include <iostream>
using namespace std;

//preprocessor

void main() //Main Function


{
cout<<X;
cout<<Y;
}

Mind map for FUNCTION in C+


+

Main function
Function

Predefined/built in
function

Function that do not


return value

User define function


Function that return a
value

Main Function
A main function is automatically execute first

when a program start.


Inside a main function, we call/invoked

another function.

Predefined/Built-in
Function
You already experience some predefined

function/standard function like


i) sqrt(), sin(), cos(), log() in math.h
ii) Setfill(*), setw(8) in iomanip.h

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

User defined function


User defined function is used to perform some

operation that is not a predefined function in


C++, such as to calculate the area of CUBE,
we can defined our own function.

Function that return a


value
Function definition/syntax

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

Function that return a


value
Function definition/syntax

ii) Function body/block


//function header
<return data type><function
name>(<parameter list>)
{
//local variable&constant
<data type> <variable identifier>;
//function body
Statement 1;
Statement 2;
return ();
}

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

int kuasa3(int x)2. Accept x


{
int z; //local variable
z=x*x*x;
return (z);
}

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

calculate area of circle.

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

Function that do not return value


Function that do not return value often written

to perform some specific task.


When a function is not returning a value to
calling program, you must use the keyword:
i) Void as return data type
ii) Return(0);
)Example, a function that will display a
header/template on the computer screen:
Name
street
address
state city
postcode

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);
}

Actual argument vs formal argument


void main()
{
int x, jawapan;
cin>>x;
Jawapan=kuasa3(x);
cout<<jwp;
}
//function definition
//function header
int kuasa3(int y)
{
int jwp, y;
Jwp=y*y*y
return(jwp);
}

X is actual argument

y is formal argument

Actual argument vs formal


argument
Actual argument and formal argument may have

different names, but they are same.


Actual argument sending the data
Formal argument are place holder for actual
argument value during the execution of function.
Actual argument must be define in calling program.
Formal argument must be define in function header
The datatype of both must SAME!
The number of actual argument and formal argument
must be same and in correct order.

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

Calling Function by value


Value parameters
Actual argument value in calling program are
actually passed(by value) to the formal
argument in the function.
A value parameter provides for one way
communication of data from the calling
program to the function.
The formal argument receive a copy of the
actual argument , thus any manipulation of
the formal argument within the function does
not affect the actual 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;
}

Actual argument is passing parameter


by value to formal argument

Formal argument is receiving a copy


from actual argument

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

communication of data between the calling program


and function
The actual argument are passed to the formal
argument in the function and after all the processing
by the function, the formal argument are passed back
to the actual argument.
This allows the function to change the actual
arguments values.
To create a reference parameter, insert the ampersand
& in front of the parameter identifier.
void PassByReference(int&x, int &y)

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;
}

Actual argument is passing parameter


by reference to formal argument

Formal argument is receiving


real parameter from actual
argument

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;

funct_ref(x, y); //call


function

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

calls itself until a primitive state is reached.


Recursivity is the property that functions have to be
called by themselves. It is useful for many task, like
sorting or calculate the factorial of numbers, compound
interest.
Example 1, to obtain the factorial of a number, says 5!
The mathematical formula would be 5!
=5*4*3*2*1*=120
More concretely, the factorial of a number, n! would be:
n!=n*(n-1)*(n-2)*(n-3)*1, where 1 in this case is
primitive case.

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);

Enter a number to find factorial:


4
Factorial of 4 = 24

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

Vous aimerez peut-être aussi