Function Definition - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Function Definition

Description:

toss = 1 rand() % 6; // toss a die yields a value between 1 and 6. rand( ) % n generates a random number ... r = (double) rand() / ((double)RAND_MAX) ... – PowerPoint PPT presentation

Number of Views:158
Avg rating:3.0/5.0
Slides: 18
Provided by: toshi155
Category:

less

Transcript and Presenter's Notes

Title: Function Definition


1
Function Definition
ltreturn typegt ltfunction namegt (ltparameter-declarat
ion-listgt) ltfunction bodygt
int gcd ( int v1, int v2 ) // return the
greatest common divisor while ( v2)
int temp v2 v2 v1
v2 v1 temp
return v1
A common divisor (gt0) of integers a and b
divides both. 12 and 16 has common divisors 1,
2, 4. 4 is their greatest common divisor.
2
The Euclidean Algorithm
Find the greatest common divisor of 184 and 69
dividend quotient divisor remainder
184 2 69 46
69 1 46 23
46 2 23 0
gcd (184, 69) gcd (69, 184 2 69) gcd
(69, 46) gcd (46, 69
46) gcd(46, 23)
gcd(23, 46 2 23) 23.
gcd(144, 693) ?
3
Computing GCD
gcd(a, b), where a gt b are positive integers can
be be computed as follows. Let r0 a, r1 b.
r0 q1 r1 r2 , 0 lt r2
lt r1 r1 q2 r2 r3 , 0 lt
r3 lt r2 r2 q3 r3 r4 ,
0 lt r4 lt r3
... r q r 0
n-1 n n
4
A Few Notes
If the function does not return a value, its
return type is void.
const char BELL \a void ring (int k)
int i for (i 0 i lt k i) cout ltlt
BELL
The function is invoked at run-time unless it has
been declared inline.
int main() int n cout ltlt \n Input a
small positive integer cin gtgt n
ring(n)
5
Function Declaration
Also referred to as function prototype.
ltreturn typegt ltfunction namegt (ltparameter listgt)
int abs( int ) int min( int, int ) int gcd(
int, int )
The interfaces of these functions.
6
Function Return Type
A predefined type (e.g., int or double), a
compound type (e.g., int or double), a
user-defined type (e.g., a class), or void
include ltstringgt include ltvectorgt class Date
/ definition / bool look_up ( int , int )
double calc ( double ) int count ( const
string char ) Date calendar ( const char )
void sum (vectorltintgt, int ) int10
foo_bar() // illegal array cannot be a return
type int foo_bar() // ok pointer to first
element of the array const is_equal (int i1, int
i2) // error missing return type
7
Function Parameter List
The parameter list of a function can be omitted.
int fork() // implicit void
parameter list int fork ( void ) //
equivalent declaration int manip( int v1, v2 )
// error int manip( int v1, int v2 )
// ok
No two parameter names can be the same.
8
Argument Passing
Initialize the storage of the function parameters
with the values of the function call arguments.
var
run-time stack
param
Pass-by-value (call-by-value) copying the
values of the arguments into the storage of the
parameters.
The contents of the arguments are not changed
under pass-by-value.
9
Pass by value
void foo( int x ) cout ltlt x ltlt endl
x 4 void main() int
y 5 foo(y) y
// 5
5
x
4
5
6
y
run-time stack
10
Unsuitable Situations for Pass-by-value
1. When a large struct/class object must be
passed as an argument
struct Large public
// 10 public members private
// 10 private members ... int func (Large
p) Large a func (a) // copy 20
members of a over to p
11
Contd
2. When the values of the arguments must be
modified
void swap ( int v1, int v2 ) int tmp
v2 v2 v1 v1 tmp
swap ( i, j ) // does not swap the value
of i, j
Solution 1 use references.
Solution 2 use pointers.
void rswap ( int v1, int v2 ) int tmp
v2 v2 v1 v1 tmp
rswap ( i, j )
void pswap ( int v1, int v2 ) int tmp
v2 v2 v1 v1 tmp
pswap ( i, j )
12
Call-by-address --- Use of References
// return status of access. place value in
parameter bool get_next_value (int next_value)
An invocation
int ival while (get_next_value (ival)) //
value of ival may be modified
// during the call
--- call by reference
The parameter binding is equivalent to the
definition int next_value ival
13
Reference Parameters
- to change the values of arguments
void rswap ( int v1, int v2 )
- to return additional results to the calling
function - to pass large class objects to a
function
Declare the parameter as a reference to a const
if it is not intended to be modified within the
function.
class X extern int foo_bar ( X) int foo(
const X xx) return foo_bar ( xx ) //
error const passed to a non-const
14
Inline Functions
Expanded in line at each point in the program
where such a function is invoked.
int min ( int v1, int v2 ) return ( v1 lt
v2 ? v1 v2 )
Calling the function is slower than directly
evaluating the conditional operator!
void main() int i1, j2, k3
int m, n m min(i,j) n
min(j,k)
void main() int i1, j2, k3
int m, n m i lt j ? i j n
j lt k ? j k
equivalent to
inline int min ( int v1, int v2) return (
v1 lt v2 ? v1 v2)
15
Example A Card Flush
Estimate the number of card flushes out of a
given number of deals.

A flush refers to the situation where at least
five cards in the hand are of the same suit.
?A ?5 ?10 ?J ?3
Program components
- poker data structure (suits, pips, cards,
deck) - poker operations (shuffling, dealing,
initializing a deck) - flush count (main( ))
16
Random Number Generation
rand( ) n generates a random number in the
range 0..n ?1.
The generator needs to be seeded first with the
statement
srand(seed) // seed is some integer
include ltstdlibgt // includes the
random number generation utility include lttimegt
// includes time utilities int toss
srand(time(NULL)) // seed the random
number generator toss 1 rand() 6 // toss
a die yields a value between 1 and 6
17
Random Floating Point Number
rand( ) generates a random integer in the range
0..RAND_MAX.
How to generate a random float in the range
xmin, xmax?
include ltstdlibgt // includes the
random number generation utility include lttimegt
// includes time utilities double r
srand(time(NULL)) // seed the generator
only once // output 100 random floating point
numbers for (int i 0 i lt 100 i) r
(double) rand() / ((double)RAND_MAX) // random
float in 0, 1 cout ltlt r (xmax - xmin)
xmin ltlt endl
poker1.h and flush_count.cpp from
Object-oriented Programming using

C by Ira Pohl, 2nd ed., Addison-Wesley
Write a Comment
User Comments (0)
About PowerShow.com