Professional Documents
Culture Documents
PART 2
REVISE
Write a function that:
a. Accept arguments and return value.
b. Accept arguments and does not return value.
c. Do not accept any argument and do not return a value.
void main()
{
float jejari;
cout<<“masukkan nilai jejari:”;
cin>>jejari;
cout<<“Luas bulatan ialah “<<luas_bulatan(jejari);
}
Exercises
1. Declare a function name calcArea, return
double value and receives three float values.
2. Declare a function name displayResult,
does not return any value and receives an
integer number.
3. Declare a function name totalMarks, return
and receive one integer value.
#include<iostream>
using namespace std;
void message1()
{
cout<<"Diploma"<<endl;
}
void message2()
{
cout<<"Teknologi"<<endl;
}
void message3()
{
cout<<"Maklumat"<<endl;
}
void main()
{
message1();
cout<<"Programming"<<endl;
message3();
message2();
}
Local and Global Variable
Local variables
Known only in the function in which they are defined
All variables declared inside a function are local
variables
Parameters
Local variables passed to function when called
(passing-parameters)
Variables defined outside and before function main:
Called global variables
Can be accessible and used anywhere in the entire
program
Local vs Global Variables
#include<iostream.h>
int x,y; //Global Variables
int add2(int, int); //function prototype
main()
{ int s;
x = 11;
y = 22;
cout << “global x=” << x << endl;
cout << “Global y=” << y << endl;
s = add2(x, y);
cout << x << “+” << y << “=“ << s;
cout<<endl;
cout<<“\n---end of output---\n”;
return 0;
} global x=11
int add2(int x1,int y1) global y=22
{ int x; //local variables Local x=44
x=44;
cout << “\nLocal x=” << x << endl; 11+22=33
return x1+y1; ---end of output---
}
Pass by Reference
also called pass by address.
The caller passes the address of the value.
allow functions to modify the value of
argument.
a copy of the address of the actual parameter
is stored. Use call by reference when you are
changing the parameter passed in by the
client program.
Pass by Reference
In pass by reference, we declare the function
parameters as references.
Example:
void AddOne(int &y) // y is a reference variable
{
y = y + 1;
}
When the function is called, y will become a
reference to the argument.
Pass by Reference (Example)
void foo(int &y) // y is now a reference Parameter in
{ function foo is a
using namespace std;
cout << "y = " << y << endl;
reference instead of
y = 6; a normal variable.
cout << "y = " << y << endl; When we call
} // y is destroyed here foo(x), y becomes a
reference to x.
int main()
{
int x = 5; Output:
cout << "x = " << x << endl; x=5
foo(x); y=5
cout << "x = " << x << endl; y=6
return 0;
x=6
}
Pass by Reference (Example)
void AddOne(int &y)
{ Output:
y++;
x=5
}
x=6
int main()
{ As you can see, the
int x = 5; function was able to
cout << "x = " << x << endl;
change the value of
AddOne(x); the argument.
cout << "x = " << x << endl;
return 0;
}
Function Call Example
#include<iostream.h>
int squareVal(int); //prototype call by value function
void squareRef(int &); // prototype call by –reference function
int main()
{ int x=2; z=4;
cout<< “x=“ << x << “before calling squareVal”;
cout << “\n” << squareVal(x) << “\n”; // call by value
cout<< “x=“ << x << “After returning”
cout<< “z=“ << z << “before calling squareRef”;
squareRef(z); // call by reference
cout<< “z=“ << z<< “After returning squareRef”
return 0;
} x=2 before calling squareVal
int squareVal(int a) 4
{ x=2 after returning
return a*=a; // caller’s argument not modified
z=4 before calling squareRef
z=16 after returning squareRef
}
void squarRef(int &cRef)
{
cRef *= cRef; // caller’s argument modified
}
Recursion and Recursive Functions
void function ()
{
if ()
cout<< ;
else
cout<< ;
function ();
}
void main ()
{
function ();
}
Recursion Example 1
An example of factorial function.
int myFactorial( int integer)
{
if( integer == 1)
return 1;
else
{
return (integer * (myFactorial(integer-1)));
}
}
Recursion Example 2
An example to print numbers counting down:
void print(int p)
{
if (p==0)
return;
cout<<p;
print(p-1);
return;
}
Exercise
Write a program with a function that takes two
int parameters, adds them together, then
returns the sum. The program should ask the
user for two numbers, then call the function
with the numbers as arguments, and tell the
user the sum.
Answer without using function
#include <iostream>
using namespace std;
void main ()
{
int number1, number2, sum;