0% found this document useful (0 votes)
27 views59 pages

C++ Functions and Arrays Overview

Uploaded by

th590668
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views59 pages

C++ Functions and Arrays Overview

Uploaded by

th590668
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

03.

Functions, Arrays,
Strings and Parameter
Passing - 01
Oritented Object Programming C++: Chapter 03

 Time: 120 Minutes


 Instructor: Thang, Nguyen Chien
 Email: chienthangplus@[Link]
 Phone: 0349 688 571
Previous lessons

1. Introduction C++
 C++ vs C
 Compilers, IDEs
2. C++ Language Basics
 Types: int, float, double
 Control Structures: if, for, while, switch-case…

Duy Tan University


2
Contents

I. Functions
 Remind how to use functions
II. Function Overloading
 What-When-How
III. Default Arguments
 What-When-How
IV. Arrays
 What-When-How

Duy Tan University


3
Objectives

 Explain how a function works


 Explain function prototyping
 Demonstrate how function overloading
 Determine how default arguments are used with
functions
 Explain recursion with the help of an example
 Explain array initialization with example
 Demonstrate how passing arrays to functions

Duy Tan University


4
I. Functions

 A function is a set of instructions that can perform a


specific task, and can be called by others.
 Benefits:
Reusability
Modularity
Overall programming simplicity
 Functions are classified into:
Library functions
User-defined functions

Duy Tan University


5
I. Functions (cont.)

 Function definition
<return type> <function name>(<argument list>)
{
<body of the function>
}
 <return type> is void when the function return
no values
 return statement used in the body to exit a function
and return a value.
 In void functions, do not need the return
 <argument list>: can be empty
Duy Tan University
6
I. Functions (cont.)

 Example 01:
void showRectangle() {
cout << "****************" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "****************" << endl;
}
Output
int main() {
****************
showRectangle(); * *
return 0; * *
****************
}

Duy Tan University


7
I. Functions (cont.)

 Example 02:
int maxTwoNumbers(int x, int y) {
if (x > y)
return x;
return y; Output
} The bigger number is 10
int main() {
int bigger, a = 2, b = 10;
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
8
I. Functions (cont.)

 Example 02:
int maxTwoNumbers(int x, int y) {
if (x > y)
return x;
return y;
} b 10
int main() { a 2
int bigger, a = 2, b = 10; bigger ???
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
9
I. Functions (cont.)

 Example 02:
int maxTwoNumbers(int x, int y) {
if (x > y)
return x;
return y;
} b 10
int main() { a 2
int bigger, a = 2, b = 10; bigger ???
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
10
I. Functions (cont.)

 Example 02: int x = a, int y = b

int maxTwoNumbers(int x, int y) {


if (x > y)
return x;
y 10
return y;
x 2
} b 10
int main() { a 2
int bigger, a = 2, b = 10; bigger ???
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
11
I. Functions (cont.)

 Example 02:
int maxTwoNumbers(int x, int y) {
if (x > y)
return x;
y 10
return y;
x 2
} b 10
int main() { a 2
int bigger, a = 2, b = 10; bigger ???
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
12
I. Functions (cont.)

 Example 02:
int maxTwoNumbers(int x, int y) {
if (x > y)
…. 10
return x;
y 10
return y;
x 2
} b 10
int main() { a 2
int bigger, a = 2, b = 10; bigger ???
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
13
I. Functions (cont.)

 Example 02:
int maxTwoNumbers(int x, int y) {
if (x > y)
…. 10
return x;
y 10
return y;
x 2
} b 10
int main() { a 2
int bigger, a = 2, b = 10; bigger ???
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
14
I. Functions (cont.)

 Example 02:
int maxTwoNumbers(int x, int y) {
if (x > y)
…. 10
return x;
return y;
} b 10
int main() { a 2
int bigger, a = 2, b = 10; bigger 10
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
15
I. Functions (cont.)

 Example 02:
int maxTwoNumbers(int x, int y) {
if (x > y)
Output
…. 10
return x;
return y; The bigger number is 10
} b 10
int main() { a 2
int bigger, a = 2, b = 10; bigger 10
bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
Duy Tan University
16
I. Functions (cont.)

 Function Declaration
Describes the function interface to the compiler
When a function is called
• The compiler uses the template to ensure that proper
arguments are passed, and the return value is correctly
<return type> <function name>(<argument list>);
 Examples:
 int maxTwoNumbers(int x, int y);
 int maxTwoNumbers(int, int);
 void printOddNumbersFrom(int k);

Duy Tan University


17
I. Functions (cont.)

 Example 03:
int maxTwoNumbers(int x, int y);
int main() {
int a = 2, b = 10;
int bigger = maxTwoNumbers(a, b);
cout << "The bigger number is " << bigger;
return 0;
}
int maxTwoNumbers(int x, int y) {
if (x > y)
return x;
return y;
}
Duy Tan University
18
Contents

I. Functions
II. Function Overloading
III. Default Arguments
IV. Arrays

Duy Tan University


19
II. Function Overloading

 Using the same function name but different


parameters to create functions that perform a variety
of different tasks
 Example: get average of 2, 3 or 4 numbers
float getAverage(float a, float b) {
return (a + b) / 2;
}
float getAverage(float a, float b, float c) {
return (a + b + c) / 3;
}
float getAverage(float a,float b, float c,float d) {
return (a + b + c + d) / 4;
}
Duy Tan University
20
II. Function Overloading (cont.)
int main() {
// invoke getAverage(float a, float b)
cout << getAverage(1.2, 4.5) << endl;
// invoke getAverage(float a, float b, float c)
cout << getAverage(1.6, 3.6, -7.5) << endl;
// invoke:
// getAverage(float a, float b, float c, float d)
cout << getAverage(4.5, 1.5, 6.5, 1.9)<< endl;
return 0;
Output
}
2.85
-0.766667
3.6

Duy Tan University


21
Contents

I. Functions
II. Function Overloading
III. Default Arguments
IV. Arrays

Duy Tan University


22
III. Default Arguments

 In C++ functions:
 Arguments can have default values from right to left
 Default values are specified when the function is declared.
 The function assigns a default value to the parameter which
does not have a matching argument in the function call
 Example:
 int sum(int a, int b, int c = 0); // OK
 int sum(int a, int b = 0, int c); // Not OK
 int sum(int a, int b = 0, int c = 0);// OK
 int sum(int a = 0, int b, int c = 0);//Not OK

Duy Tan University


23
III. Default Arguments (cont.)

 Example 01:
float sum(float a, float b, float c = 0,
float d = 0) {
return (a + b + c + d);
Output
} sum(1.2, 4.5, 0, 0) 2.85
int main() { -0.766667
cout << sum(1.2, 4.5) << endl; 3.6
cout << sum(1.6, 3.6, -7.5) << endl;
cout << sum(4.5, 1.5, 6.5, 1.9)<< endl;
return 0;
}
getAverage(1.2, 4.5, -7.5, 0)

Duy Tan University


24
III. Default Arguments (cont.)

 Example 02:
float sum(float, float, float = 0, float = 0);
int main() {
cout << sum(1.2, 4.5) << endl;
cout << sum(1.6, 3.6, -7.5) << endl;
cout << sum(4.5, 1.5, 6.5, 1.9)<< endl;
return 0;
}
float sum(float a, float b, float c, float d)
{
return (a + b + c + d);
}

Duy Tan University


25
Contents

I. Functions
II. Function Overloading
III. Default Arguments
IV. Arrays

Duy Tan University


26
Practices by Your-Self in 10 minutes

Duy Tan University


27
Review 01

int Fibonacci(int n) {
if (n == 1 || n == 2)
return 1;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
int main() {
Output
cout << Fibonacci(5) << endl;
5
return 0;
}

Duy Tan University


28
Review 02
void func() { Output
int a = 1; 2|2
static int sa = 1; 2|3
a = a + 1; 2|4
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0;
}
Duy Tan University
29
Review 02 (cont.)
void func() {
int a = 1;
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0;
}
Duy Tan University
30
Review 02 (cont.)
void func() {
int a = 1;
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0;
} a 1

Duy Tan University


31
Review 02 (cont.)
void func() {
int a = 1;
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 1
} a 1

Duy Tan University


32
Review 02 (cont.)
void func() {
int a = 1;
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 1
} a 1
2

Duy Tan University


33
Review 02 (cont.)
void func() {
int a = 1;
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 1
2
} a 2

Duy Tan University


34
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 2
} a 2

Duy Tan University


35
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 2
} a 2

Duy Tan University


36
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 2
}
Duy Tan University
37
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 2
} a 1

Duy Tan University


38
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 2
} a 1
2

Duy Tan University


39
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1;
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 2
3
} a 2

Duy Tan University


40
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1; 2|3
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 3
} a 2

Duy Tan University


41
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1; 2|3
a = a + 1;
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 3
} a 2

Duy Tan University


42
Review 02 (cont.)
void func() { Output
int a = 1; 2|2
static int sa = 1; 2|3
a = a + 1; 2|4
sa = sa + 1;
cout << a << " | " << sa << endl;
}
int main() {
func();
func();
func();
return 0; sa 4
3
}
Duy Tan University
43
Review 03
void func(int a, int b, int c) {
cout << a << " " << b << " " << c << endl;
} Output
int main() {
321
int i = 1;
func(i++, i++, i++);
return 0;
}

Duy Tan University


44
Contents

I. Functions
II. Function Overloading
III. Default Arguments
IV. Arrays

Duy Tan University


45
IV. Arrays

 An array in C++ is a collection of elements


Elements stored at contiguous memory locations
Elements can be accessed randomly using indices
of an array.
 Declaration for an array: type name [elements];
 Example:
int arr[4];
0 1 2 3
arr
int

Duy Tan University


46
IV. Arrays (cont.)

 Initializing arrays:
 int arr1[4] = {5, 7, 1, 2};
0 1 2 3
arr1 5 7 1 2

 int arr2[4] = {9, 2};


0 1 2 3
arr2 9 2 0 0

 int arr3[] = {7, 3, 5, 1, 2};


0 1 2 3 4
arr3 7 3 5 1 2

Duy Tan University


47
IV. Arrays (cont.)

 Array elements are accessed by using an integer


index.
 Array index starts with 0 and goes till size of array minus 1
 Syntax: name [index];
 Example:
int arr[4] = { 5, 7, 1, 2 };
for (int i = 0; i < 4; i++) {
Output
cout << arr[i] << " ";
5712
}
arr[0] arr[1] arr[2] arr[3]
arr 5 7 1 2

Duy Tan University


48
IV. Arrays (cont.)

 Multidimensional arrays can be described as "arrays


of arrays". arr2D[1]
 Example: int arr2D[3][4]; int[4]
arr2D 0 1 2 3
0
1
2

arr2D[2][3]
int
arr2D[0]
int[4]
arr2D[2] arr2D[1][1]
int[4] int
Duy Tan University
49
IV. Arrays (cont.)

 Multidimensional arrays can be described as "arrays


of arrays".
 Example: int arr[3][2][3];
int[2][3] int[2][3] int[2][3]
arr[0] arr[1] arr[2]

int[3] int[3] int[3] int[3] int[3] int[3]


arr[0][0] arr[0][1] arr[1][0] arr[1][1] arr[2][0] arr[2][1]

arr[2][0][2]
arr[0][1][0]
int
int
Duy Tan University
50
IV. Arrays (cont.)

 Initializing multidimensional arrays


int arr2D[2][3] = { {3, 2, 4}, {8, 2, 5} };
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr2D[i][j] << " ";
}
cout << endl;
Output
}
324
arr2D 0 1 2 825
0 3 2 4
1 8 2 5

Duy Tan University


51
IV. Arrays (cont.)

 Example 01: Accessing an out-of-bounds value


int main() {
int arr[3] = { 2, 1, 9 }; Output
cout << arr[4]; 11533456
return 0;
}

arr[0] arr[1] arr[2] arr[4]


….. 2 1 9 ??? …..

Duy Tan University


52
IV. Arrays (cont.)

 Example 02: Initializing multidimensional arrays


int arr2D[3][3] = { {3, 2}, {8} };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << arr2D[i][j] << " ";
}
cout << endl;
Output
}
320
arr2D 0 1 2 800
0 3 2 0 000
1 8 0 0
2 0 0 0
Duy Tan University
53
IV. Arrays (cont.)

 Example 03: Passing arrays to a function


int sum(int arr[], int size) {
int s = 0;
for (int i = 0; i < size; i++)
s += arr[i];
return s; Output
} size: 3
int main() { sum: 12
int arr[3] = { 2, 4, 6 };
int size = sizeof(arr) / sizeof(arr[0]);
cout << "size: " << size << endl;
cout << "sum: " << sum(arr, size);
return 0;
} 54 Duy Tan University
IV. Arrays (cont.)

 Example 04: Passing multidimensional arrays


int sum(int arr[][3][2], int size) {
int s = 0;
...
return s;
}
int main() {
int arr[3][3][2] = {{{1,2},{3,2},{1,5}},
{{7,2},{8,1}} };
cout << "sum: " << sum(arr, 5);
return 0;
}
Duy Tan University
55
IV. Arrays (cont.)

 Example 05: Accessing an out-of-bounds value


int main() {
int arr[4][2] = {{2,9}, {3,2}, {7,2}};
cout << "arr[0][4]: " << arr[0][4];
return 0; Output
} arr[0][0] arr[0][1] arr[0][4] arr[0][4]: 7
int int int

2
1 9
2 3 2
4 7
5 2
6 0
7 0
8
arr[0] arr[1] arr[2] arr[3]
int[2] int[2] int[2] int[2]

Duy Tan University


56
Contents

I. Functions
II. Function Overloading
III. Default Arguments
IV. Arrays

Duy Tan University


57
Summary

 Function declaration and definition likes C


 Function overloading: functions have a same name
 Arguments have default values, the values will be
assigned for parameters which does not have a
matching argument in function call
 An array in C++ is a collection of elements
 Elements stored at contiguous memory locations
 Elements can be accessed randomly using indices of an
array.
 Multidimensional arrays can be described as "arrays
of arrays".
Duy Tan University
58
Problems

1. Write a function to get the largest values in an array


2. Write input(…) and display(…) functions to get
values from the keyboard and display them in to the
screen.
3. Write a function to display all prime numbers in an
array.
4. Write iSort(…) function to sort elements increasing
in an array
5. Write iInsert(…) function to insert an element to a
sorted array.

Duy Tan University


59

You might also like