You are on page 1of 25

DFC2073 - FUNCTION

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.

Write a function that call user defined functions:


 a. Call function with the constant value.
 b. Call function with variables as arguments.
 c. Call function which does not return any value.
 d. Call function with no arguments and does not return any value.
Objectives:
By the end of this lesson, you will be able to:
Use the parameters passing techniques.

1. Describe two ways by which function can receive


parameters:
 a. Pass by value
 b. Pass by reference

2. Write a function based on both techniques.


3. List the difference between ‘Pass by value’ and ‘Pass
by Reference’.
Parameters passing techniques

 Parameters passing techniques can


be classified into two types. They are:
 a. Pass by value
 b. Pass by reference
Pass by value
 Pass by value – Direct Value
 It is possible to pass values from one
function to another function for performing
calculations. This type of a function call is
referred to as Pass by value
Program to illustrate how to pass a
value to a function.
// Program to increment the value by one
#include <iostream.h>
void main()
{
int incre(int);
int result; function.
cout<<"\n Before incrementing : 4";
result=incre(4);
cout<<"\n After incrementing :“<<result;This statement will pass the value 4 to the incre function.
}
int incre(int a)
{
return(++a);
}
This statement will return the value back to main function after
incrementing the value of a.
Output
Before incrementing : 4
After incrementing : 5
 The calling function main passes the value 4
as an argument to the function incre.
 This value is assigned to the variable a in the
called function.
 The value of the variablea is incremented and
is returned to the main function.
 The incremented value is displayed from the
main function.
Pass by Value – Through Variables
Example of program
/* Program to add two numbers using function */
#include <iostream.h>
function prototype
void main()
{
int add(int,int); Function declaration
int sum,a,b;
cout<<"\n\n Enter two values : ";
cin>>a>>b;
sum = add(a,b); Calling function
cout<<"\n The sum of a and b is:”<<sum;
}
int add(int x,int y) Called function
{
return(x+y); Function definition
}
//Example program to illustrate the working of a function
#include <iostream.h>
float luas_bulatan (float jejari)
{
float luas, pai = 3.142;
luas = jejari * jejari * pai;
return luas;
}

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

 Main calls another function…..normal


 A function calls another function2….normal
 A function calls itself ?! Possible?? YES
A recursive function is one that call itself.
Concept Of recursion
 A recursive function is called to solve a problem
 The function knows to solve only the simplest cases
or so-called base-cases
 Thus if the function called with a base-case, it simply
returns a result. But if it is called with more complex
problem, the function divides the problem into two
conceptual pieces, one knows how to do, and another
doesn't know what to do.
 The second case/piece must resemble the original
problem, but be a slightly simpler/smaller version of
the original problem
EXAMPLE CODING

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;

cout << "Enter two integers:\n";


cin >> number1 >> number2;
sum = number1+ number2;
cout << "\nThe sum is " << sum << ".";
}

You might also like