Anda di halaman 1dari 25

Dr.

Siti Barirah Ahmad Anas


barirah@eng.upm.edu.my
03-89466439
Room A.04.89
Functions
Technology Oriented Business Driven Sustainable Development Environmental Friendly
Functions
Calling a function
Call by value
Call by reference
Local and Global Variables
Recursive functions
Standard Library
Headers
Functions


Technology Oriented Business Driven Sustainable Development Environmental Friendly
To understand and appreciate the use of
functions in programming

To implement functions using
Call-by-value
Call-by-reference
Recursive technique

To differentiate the effects of using local
and global variables
To get familiar with some header files
and libraries
Technology Oriented Business Driven Sustainable Development Environmental Friendly
A black box where one or more input values are passed
and automatically return a single output value.
Must be declared before being referenced in program
body we call this function prototype
Must have calling function and called function

Definition of a function
A function definition has the following general form:

returnValueType functionName (parameterList)
{
local variable declarations
executable statements
return(n)
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
S
t
r
u
c
t
u
r
e
d

P
r
o
g
r
a
m
m
i
n
g

main
function 1 function 2 function 3
function a function b function c function d function a function f
Functions
called by main
Functions called by function 1
Function
called by
function 2
Functions called by
function 3
Technology Oriented Business Driven Sustainable Development Environmental Friendly
#include <iostream>
using namespace std;

int main()
{
int num, i, product = 1;

cout << "Enter an integer: ";
cin >> num;

for (i = 1;i <= num; ++i)
product *= i;

cout << "Factorial of " << num << " is " << product << endl;

return 0;
}


Technology Oriented Business Driven Sustainable Development Environmental Friendly
#include <iostream>
using namespace std;

int main(void)
{
int num, ans;
int factorial(int n);

cout << "Enter an integer: ";
cin >> num;

ans = factorial(num);

cout << "Factorial of << num << is << ans << endl;

return 0;
}

int factorial(int n)
{
int i, product = 1;

for (i=1;i<=n;++i)
product*=i;

return product;
}

Function prototype
Function call
Function definition
num ans
n
product
Technology Oriented Business Driven Sustainable Development Environmental Friendly
int factorial(int n) //function header
{ //function body
int i, product = 1;

for(i = 2; i <= n; ++i)
product *= i;

return product;
}

first int tells the compiler that the value returned by the
function factorial will be converted, if necessary, to
data type int.
int n tells the compiler that the function takes a single
argument of type int.
an expression such as factorial(5) causes the function
to be called. Effect with n having the value of 5.
Technology Oriented Business Driven Sustainable Development Environmental Friendly
void menu(void)
{
cout << \t************************\n;
cout << \t* 1. ADDITION *\n;
cout << \t* 2. SUBTRACTION *\n;
cout << \t* 3. MULTIPLICATION *\n;
cout << \t************************\n;
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
// Finding the maximum of three integers

#include <iostream>
using namespace std;

int maximum( int, int, int ); // function prototype

int main()
{
int a, b, c;
cout << "Enter three integers: ";
cin >> a >> b >> c;
cout << "Maximum is: << maximum(a,b,c) << endl;

return 0;
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
// Function maximum definition

int maximum(int x,int y,int z)
{
int max = x; // assume x is the maximum

if ( y > max )
max = y;
if ( z > max )
max = z;

return max;
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
Call by value
Copy of argument passed to function
Changes in function do not effect
original
Use when function does not need to
modify argument

Call by reference
Passes original argument
Changes in function effect original

#include <iostream>
using namespace std;

int main(void)
{
void fun(int x);
int a = 5;

fun(a);
cout << "a = " << a << endl;

return 0;
}

void fun(int x)
{
x = x + 3;

return;
}
#include <iostream>
using namespace std;

int main(void)
{
void fun(int *x);
int a = 5;

fun(&a);
cout << "a = " << a << endl;

return 0;
}

void fun(int *x)
{
*x = *x + 3;

return;
}
a 5
x
5
a 5
x
Dereference
One way
communication
call-by-value call-by-reference
Technology Oriented Business Driven Sustainable Development Environmental Friendly
// Cube a variable using call-by-value

#include <iostream>
using namespace std;

int cubeByValue( int ); // functionprototype

int main()
{
int number = 5;
cout << "The original value of number is << number;
number = cubeByValue( number );
cout << "\nThe new value of number is << number << endl;

return 0;
}

int cubeByValue( int n )
{
return n * n * n; // cube local variable n
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
// Cube a variable using call-by-reference with a pointer argument

#include <iostream>
using namespace std;

void cubeByReference( int * ); // prototype

int main()
{
int number = 5;

cout << "The original value of number is << number;
cubeByReference( &number );
cout << "\nThe new value of number is << number << endl;

return 0;
}

void cubeByReference( int *nPtr )
{
*nPtr = *nPtr * *nPtr * *nPtr; // cube number in main
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
#include <iostream>
using namespace std;

void useLocal(void); // Function prototype
int x = 5;

int main(void)
{
cout << "Global variable x is << x;
useLocal();
cout << "Global variable x is << x << endl;

return 0;
}

void useLocal(void)
{
int x = 1;
cout << Local variable x is << x;
return;
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
Function that repeatedly calls itself to perform
calculations.

Example1: computes the sum of the first n positive
integers.

int sum(int n)
{
if(n <= 1)
return n;
else
return n + sum(n-1);
}

Technology Oriented Business Driven Sustainable Development Environmental Friendly
The recursive function sum() is
analyzed as illustrated below.

Function call Value returned
sum(1) 1
sum(2) 2 + sum(1) or 2 + 1
sum(3) 3 + sum(2) or 3 + 2 + 1
sum(4) 4 + sum(3) or 4 + 3 + 2 + 1
Technology Oriented Business Driven Sustainable Development Environmental Friendly
Example2: Compute the factorial of n

int factorial(int n)
{
int result;

if(n<=1)
result=1;
else
result= n*factorial(n-1);
return result;
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
Fibonacci series
0,1,1,2,3,5,8,13,21,
Begins with 0 and 1
Has a property that each subsequent Fibonacci
number is the sum of previous 2 numbers

Can be defined recursively as:
fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
Technology Oriented Business Driven Sustainable Development Environmental Friendly
#include <iostream>
using namespace std;

long fibonacci(long n); // function prototype

int main()
{
long result, number;

cout << Enter an integer: ;
cin >> number;

result = fibonacci(number); // function call
cout << \nFibonacci( << number << ) = << result << endl;

return 0;
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
// Recursive definition of fibonacci function

long fibonacci(long n)
{
if (n == 0 || n == 1)
return n;
else
return fibonacci(n-1) + fibonacci(n-2);
}
Technology Oriented Business Driven Sustainable Development Environmental Friendly
Example:
Recursive calls for fibonacci(3)
fibonacci (3)
return fibonacci (2) + fibonacci(1)
return fibonacci (1) + fibonacci(0)
return 0
return 1
return 1
Technology Oriented Business Driven Sustainable Development Environmental Friendly
S
t
a
n
d
a
r
d

L
i
b
r
a
r
y

H
e
a
d
e
r
s

C++ Strings
<string> Provides the C++ standard string classes and templates.
C++ Streams and Input/Output
<fstream> Provides facilities for file-based input and output.
<ios> Provides several types and functions basic to the operation of iostreams.
<iostream> Provides C++ input and output fundamentals.
<iosfwd> Provides forward declarations of several I/O-related class templates.
<iomanip>
Provides facilities to manipulate output formatting, such as the base used when formatting integers
and the precision of floating point values.
<istream> Provides the template class std::istream and other supporting classes for input.
<ostream> Provides the template class std::ostream and other supporting classes for output.
<sstream> Provides the template class std::sstream and other supporting classes for string manipulation.
<streambuf>
Numerics
<complex> Provides class template std::complex and associated functions for working with complex numbers.
<numeric> Provides algorithms for numerical processing.
<valarray> Provides the template class std::valarray, an array class optimized for numeric processing.
Language Support
<exception>
Provides several types and functions related to exception handling, including std::exception, the
base class of all exceptions thrown by the Standard Library.
<limits>
Provides the template class std::numeric_limits, used for describing properties of fundamental
numeric types.
<new>
Provides operators new and delete and other functions and types composing the fundamentals of
C++ memory management.
<typeinfo> Provides facilities for working with C++ run-time type information.
Technology Oriented Business Driven Sustainable Development Environmental Friendly
S
t
a
n
d
a
r
d

L
i
b
r
a
r
y

F
u
n
c
t
i
o
n
s

(
c
m
a
t
h
)

Function Description Example
sqrt(x)
square root of x
sqrt(4.0) is 2.0
log10(x)
logarithm of x (base 10)
log10(100) is 2
pow(x, y)
x raised to power y ( x
y
)
pow(2, 5) is 32
fabs(x)
absolute value of x
fabs(-5.0) is 5.0
sin(x)
Trigonometric sine of x
(x in radians)
sin(0.0) is 0.0

Anda mungkin juga menyukai