Professional Documents
Culture Documents
W02
Review of C++ - Functions
Function Definition
Function Prototype
Function Calls
Default Parameters
Overloaded Functions
Inline Functions
Recursion
2
Functions
W02-Review of C++
3
Functions
W02-Review of C++
4
Function Definition
W02-Review of C++
5
W02-Review of C++
6
An Example
W02-Review of C++
7
W02-Review of C++
8
Function Prototypes
Examples:
double sqrt(double);
int add2nums(int, int);
int counter(void);
W02-Review of C++
9
Using a Prototype
int counter(void);
int main(void) {
cout << counter() << endl;
cout << counter() << endl;
cout << counter() << endl;
}
int counter(void) {
static int count = 0;
count++;
return(count);
}
W02-Review of C++
10
Function Calls
Function Call-by-Value
The value of a variable is passed in
Caller sends a copy of data value.
Any change in data value is not visible in caller.
Function Call-by-Reference
The reference to a variable is passed in.
The function can change callers variables directly.
This change is visible in the caller.
W02-Review of C++
11
W02-Review of C++ 12
References
W02-Review of C++
13
int &first;
double &last;
char &c;
W02-Review of C++
14
int count;
int &c = count;
// c is the same variable as count
count = 1;
cout << “c is “ << c << endl;
c++;
cout << “count is “ << count << endl;
W02-Review of C++
15
Reference Parameters
W02-Review of C++
16
int main ()
{
int x = 1, y = 3, z = 7;
duplicate (x, y, z);
cout << "x =" << x << ", y =" << y << ", z =" << z;
return 0;
}
W02-Review of C++
17
W02-Review of C++
18
Default Parameters
W02-Review of C++
19
Default Parameters
int divide (int a, int b=2)
{
int r;
r = a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
W02-Review of C++
20
Overloaded Functions
W02-Review of C++
21
W02-Review of C++
22
Inline Functions
Example
inline double cube( double s )
{ return s * s * s; }
W02-Review of C++
23
int main() {
int i1 = 5, i2 = 6;
cout << max(i1, i2) << endl;
return 0;
}
W02-Review of C++
24
Recursion
Functions can call themselves! This is called
recursion.
Example:
long int factorial( int x ) {
if (x == 1)
return(1);
else
return(x * factorial(x-1));
}
W02-Review of C++
25
W02-Review of C++
26
Recursion
W02-Review of C++
27
W02
Review of C++ - Arrays
W02-Review of C++
29
Arrays
W02-Review of C++
30
Arrays in Memory
4 bytes
num[0]
num[1]
int num[6];
num[5]
W02-Review of C++
31
W02-Review of C++
32
Array Subscripts
W02-Review of C++
34
Declaring An Array
elementType arrayName[numberOfElements];
W02-Review of C++
35
Array Example
int factorial( int n ) {
if (n == 1)
return(1);
int main(void) { else
int facs[10]; return(n * factorial(n-1));
}
for (int i=0;i<10;i++)
facs[i] = factorial(i);
Initialization
You can initialize an array when you declare it:
2D Arrays
char A[3][4];
W02-Review of C++
39
2D Arrays
Row Major:
• A is an array of size 4.
A[1]
{ A[1][0]
A[1][1]
A[1][2]
{
an array of 3 chars
A[3][0]
A[3] A[3][1]
A[3][2]
W02-Review of C++
40
2D Array Example
const int r = 2;
const int c = 3;
2D Array Example
const int NumStudents = 10;
const int NumHW = 3;
double grades[NumStudents][NumHW];
2D Array Example
W02-Review of C++
43
W02-Review of C++
44
Spring 2016
W02
Review of C++: Pointers
Declaration
Derefrencing Pointers
Pointers and Arrays
Pointer Arithmetic
45
Pointers
MEMORY
Address
A pointer is a variable that holds
0 the address
of another variable. 1
2
3
num
int num; 4 123
5
int *x;
...
...
W02-Review of C++
46
Pointer Variables
int **y;
y some *int some int
double *z;
z some double
W02-Review of C++
47
Dereferencing a Pointer
W02-Review of C++
48
Pointer Example
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
W02-Review of C++
49
int *x;
int a[10]; x is “the address of a[2] ”
x = &a[2];
W02-Review of C++
50
Pointer Arithmetic
W02-Review of C++
51
Example
int main ()
{
int numbers[5];
int* p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
W02-Review of C++
52
Printing an Array
void printArray (int A[], int len) {
for (int i=0;i<len;i++)
cout << "[" << i << "] = "
<< A[i] << endl;
}
W02-Review of C++
53
W02-Review of C++
54