Sie sind auf Seite 1von 93

Chapter 2.

C++ BASICS

Chapter 2. C++ BASICS


Minh Quang Nguyen
Hanoi National University of Education

September 2015

Chapter 2. C++ BASICS

The structure of a C++ program


Input/output stream
Operator
Fundamental statement: loop and conditional statement
FUNCTIONs

Chapter 2. C++ BASICS


The structure of a C++ program

An example

There must be one and


only one function name
main in a program.

#include <iostream.h>
using namespace std;
int main()
{
cout << \Hello world \n";
return 0;
}

Statement is isolated by
;

#include: add a library


into source code

using namespace: add a


library into source code

Comment in one line, using


//, in multiple lines using
/* comment */

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
A variable is a storage location paired with an associated symbolic
name (an identifier), which contains some known or unknown
quantity of information referred to as a value.
To define a variable:
Data type variable name;
or
Data type variable name = value;
Example:
int a;
int a, b = 10;

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I

A variable must be defined before any use

Definition for a variable can be located anywhere in a


program.
Naming conventions:

I
I

Case sensitive
Only contains: a-z, A-Z, 0-9 and

Example:
int total_student;
char _student_name;

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I

A variable must be defined before any use

Definition for a variable can be located anywhere in a


program.
Naming conventions:

I
I

Case sensitive
Only contains: a-z, A-Z, 0-9 and

Example:
int total_student;
char _student_name;

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I
I

Integer - int, long, short


Character - char: store ASCII value of a charater.
I
I

It must be in .
Special charater:
n,
tab,
,...

Unsigned numbers: unsigned char, unsigned in,


unsigned short, unsigned long

Real number: float, double, long double

bool: true/false

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I
I

Integer - int, long, short


Character - char: store ASCII value of a charater.
I
I

It must be in .
Special charater:
n,
tab,
,...

Unsigned numbers: unsigned char, unsigned in,


unsigned short, unsigned long

Real number: float, double, long double

bool: true/false

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I
I

Integer - int, long, short


Character - char: store ASCII value of a charater.
I
I

It must be in .
Special charater:
n,
tab,
,...

Unsigned numbers: unsigned char, unsigned in,


unsigned short, unsigned long

Real number: float, double, long double

bool: true/false

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I
I

Integer - int, long, short


Character - char: store ASCII value of a charater.
I
I

It must be in .
Special charater:
n,
tab,
,...

Unsigned numbers: unsigned char, unsigned in,


unsigned short, unsigned long

Real number: float, double, long double

bool: true/false

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I
I

Integer - int, long, short


Character - char: store ASCII value of a charater.
I
I

It must be in .
Special charater:
n,
tab,
,...

Unsigned numbers: unsigned char, unsigned in,


unsigned short, unsigned long

Real number: float, double, long double

bool: true/false

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I
I

Integer - int, long, short


Character - char: store ASCII value of a charater.
I
I

It must be in .
Special charater:
n,
tab,
,...

Unsigned numbers: unsigned char, unsigned in,


unsigned short, unsigned long

Real number: float, double, long double

bool: true/false

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I
I

Integer - int, long, short


Character - char: store ASCII value of a charater.
I
I

It must be in .
Special charater:
n,
tab,
,...

Unsigned numbers: unsigned char, unsigned in,


unsigned short, unsigned long

Real number: float, double, long double

bool: true/false

Chapter 2. C++ BASICS


The structure of a C++ program

Variables
I
I

Integer - int, long, short


Character - char: store ASCII value of a charater.
I
I

It must be in .
Special charater:
n,
tab,
,...

Unsigned numbers: unsigned char, unsigned in,


unsigned short, unsigned long

Real number: float, double, long double

bool: true/false

Chapter 2. C++ BASICS


The structure of a C++ program

Variables

Type
char
short
int
long
float
double
long double

Low
-128
-32768
-2147483648
-2147483648
3.4 1038
1.7 10308
3.4 104932

High
127
32767
2147483647
2147483647
3.4 1038
1.7 10308
3.4 104932

Bytes
1
2
4
4
4
4
10

Chapter 2. C++ BASICS


The structure of a C++ program

Constant variables

Constant variable is a variable with a fix value. 2 ways to define a


constant variable:
I

const float PI = 3.14;

#define PI 3.14

Chapter 2. C++ BASICS


The structure of a C++ program

Constant variables

Constant variable is a variable with a fix value. 2 ways to define a


constant variable:
I

const float PI = 3.14;

#define PI 3.14

Chapter 2. C++ BASICS


The structure of a C++ program

Constant variables

Constant variable is a variable with a fix value. 2 ways to define a


constant variable:
I

const float PI = 3.14;

#define PI 3.14

Chapter 2. C++ BASICS


The structure of a C++ program

Constant variables

Constant variable is a variable with a fix value. 2 ways to define a


constant variable:
I

const float PI = 3.14;

#define PI 3.14

Chapter 2. C++ BASICS


The structure of a C++ program

Constant variables

Constant variable is a variable with a fix value. 2 ways to define a


constant variable:
I

const float PI = 3.14;

#define PI 3.14

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I

int i = 8;

cout << The value of i is: << i << endl;

cout << The value of i is: << i <<


n;

Homework: learn about setw - How to use it? How does it work?

Chapter 2. C++ BASICS


Input/output stream

Using cin

The standard input by default is the keyboard, and the C++


stream object defined to access it is cin For formatted input
operations, cin is used together with the extraction operator, which
is written as Example
I

int i;

cout << Input a value for i << endl;

cin i;

Chapter 2. C++ BASICS


Input/output stream

Using cin

The standard input by default is the keyboard, and the C++


stream object defined to access it is cin For formatted input
operations, cin is used together with the extraction operator, which
is written as Example
I

int i;

cout << Input a value for i << endl;

cin i;

Chapter 2. C++ BASICS


Input/output stream

Using cin

The standard input by default is the keyboard, and the C++


stream object defined to access it is cin For formatted input
operations, cin is used together with the extraction operator, which
is written as Example
I

int i;

cout << Input a value for i << endl;

cin i;

Chapter 2. C++ BASICS


Input/output stream

Using cin

The standard input by default is the keyboard, and the C++


stream object defined to access it is cin For formatted input
operations, cin is used together with the extraction operator, which
is written as Example
I

int i;

cout << Input a value for i << endl;

cin i;

Chapter 2. C++ BASICS


Input/output stream

Using cin

The standard input by default is the keyboard, and the C++


stream object defined to access it is cin For formatted input
operations, cin is used together with the extraction operator, which
is written as Example
I

int i;

cout << Input a value for i << endl;

cin i;

Chapter 2. C++ BASICS


Input/output stream

Using cin

The standard input by default is the keyboard, and the C++


stream object defined to access it is cin For formatted input
operations, cin is used together with the extraction operator, which
is written as Example
I

int i;

cout << Input a value for i << endl;

cin i;

Chapter 2. C++ BASICS


Input/output stream

Using cin

The standard input by default is the keyboard, and the C++


stream object defined to access it is cin For formatted input
operations, cin is used together with the extraction operator, which
is written as Example
I

int i;

cout << Input a value for i << endl;

cin i;

Chapter 2. C++ BASICS


Input/output stream

Header file and library file

Why we need header files?


I

It speeds up compile time.

It keeps your code more organized.

It allows you to separate interface from implementation.

Lets see an example

Chapter 2. C++ BASICS


Input/output stream

Header file and library file

Why we need header files?


I

It speeds up compile time.

It keeps your code more organized.

It allows you to separate interface from implementation.

Lets see an example

Chapter 2. C++ BASICS


Input/output stream

Header file and library file

Why we need header files?


I

It speeds up compile time.

It keeps your code more organized.

It allows you to separate interface from implementation.

Lets see an example

Chapter 2. C++ BASICS


Input/output stream

Header file and library file

Why we need header files?


I

It speeds up compile time.

It keeps your code more organized.

It allows you to separate interface from implementation.

Lets see an example

Chapter 2. C++ BASICS


Input/output stream

Header file and library file (cont.)

Two kinds of library


I

You build it yourself


#include myprog.h

In the standard directory of C++.


#include math.h

Lets see an other example

Chapter 2. C++ BASICS


Input/output stream

Header file and library file (cont.)

Two kinds of library


I

You build it yourself


#include myprog.h

In the standard directory of C++.


#include math.h

Lets see an other example

Chapter 2. C++ BASICS


Input/output stream

Header file and library file (cont.)

Two kinds of library


I

You build it yourself


#include myprog.h

In the standard directory of C++.


#include math.h

Lets see an other example

Chapter 2. C++ BASICS


Operator

Operators

Three kinds of Operator


I

Mathematical operator

Relational operator

Logical operator

Chapter 2. C++ BASICS


Operator

Operators

Three kinds of Operator


I

Mathematical operator

Relational operator

Logical operator

Chapter 2. C++ BASICS


Operator

Operators

Three kinds of Operator


I

Mathematical operator

Relational operator

Logical operator

Chapter 2. C++ BASICS


Operator

Operators

Three kinds of Operator


I

Mathematical operator

Relational operator

Logical operator

Chapter 2. C++ BASICS


Operator

Mathematical Operator
I

Addition +

Subtraction -

Multiple *

Division /

Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0

Example

Chapter 2. C++ BASICS


Operator

Mathematical Operator
I

Addition +

Subtraction -

Multiple *

Division /

Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0

Example

Chapter 2. C++ BASICS


Operator

Mathematical Operator
I

Addition +

Subtraction -

Multiple *

Division /

Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0

Example

Chapter 2. C++ BASICS


Operator

Mathematical Operator
I

Addition +

Subtraction -

Multiple *

Division /

Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0

Example

Chapter 2. C++ BASICS


Operator

Mathematical Operator
I

Addition +

Subtraction -

Multiple *

Division /

Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0

Example

Chapter 2. C++ BASICS


Operator

Mathematical Operator
I

Addition +

Subtraction -

Multiple *

Division /

Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0

Example

Chapter 2. C++ BASICS


Operator

Mathematical Operator
I

Addition +

Subtraction -

Multiple *

Division /

Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0

Example

Chapter 2. C++ BASICS


Operator

Relational Operator
Only release one in two value: TRUE or FALSE
I

Greater than:

Greater than or equal to:

Lower than:

Lower than or equal to:

Equal:

Different:
int a
float
(a ==
(a >=

Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true

Chapter 2. C++ BASICS


Operator

Relational Operator
Only release one in two value: TRUE or FALSE
I

Greater than:

Greater than or equal to:

Lower than:

Lower than or equal to:

Equal:

Different:
int a
float
(a ==
(a >=

Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true

Chapter 2. C++ BASICS


Operator

Relational Operator
Only release one in two value: TRUE or FALSE
I

Greater than:

Greater than or equal to:

Lower than:

Lower than or equal to:

Equal:

Different:
int a
float
(a ==
(a >=

Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true

Chapter 2. C++ BASICS


Operator

Relational Operator
Only release one in two value: TRUE or FALSE
I

Greater than:

Greater than or equal to:

Lower than:

Lower than or equal to:

Equal:

Different:
int a
float
(a ==
(a >=

Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true

Chapter 2. C++ BASICS


Operator

Relational Operator
Only release one in two value: TRUE or FALSE
I

Greater than:

Greater than or equal to:

Lower than:

Lower than or equal to:

Equal:

Different:
int a
float
(a ==
(a >=

Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true

Chapter 2. C++ BASICS


Operator

Relational Operator
Only release one in two value: TRUE or FALSE
I

Greater than:

Greater than or equal to:

Lower than:

Lower than or equal to:

Equal:

Different:
int a
float
(a ==
(a >=

Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true

Chapter 2. C++ BASICS


Operator

Relational Operator
Only release one in two value: TRUE or FALSE
I

Greater than:

Greater than or equal to:

Lower than:

Lower than or equal to:

Equal:

Different:
int a
float
(a ==
(a >=

Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true

Chapter 2. C++ BASICS


Operator

Relational Operator
Only release one in two value: TRUE or FALSE
I

Greater than:

Greater than or equal to:

Lower than:

Lower than or equal to:

Equal:

Different:
int a
float
(a ==
(a >=

Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true

Chapter 2. C++ BASICS


Operator

Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I

AND:

OR:

NOT:

Conditional operator condition ? true expression:false


expression
int a
float
(a ==
(a ==
int c
Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?

Chapter 2. C++ BASICS


Operator

Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I

AND:

OR:

NOT:

Conditional operator condition ? true expression:false


expression
int a
float
(a ==
(a ==
int c
Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?

Chapter 2. C++ BASICS


Operator

Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I

AND:

OR:

NOT:

Conditional operator condition ? true expression:false


expression
int a
float
(a ==
(a ==
int c
Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?

Chapter 2. C++ BASICS


Operator

Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I

AND:

OR:

NOT:

Conditional operator condition ? true expression:false


expression
int a
float
(a ==
(a ==
int c
Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?

Chapter 2. C++ BASICS


Operator

Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I

AND:

OR:

NOT:

Conditional operator condition ? true expression:false


expression
int a
float
(a ==
(a ==
int c
Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?

Chapter 2. C++ BASICS


Operator

Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I

AND:

OR:

NOT:

Conditional operator condition ? true expression:false


expression
int a
float
(a ==
(a ==
int c
Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?

Chapter 2. C++ BASICS


Operator

Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I

AND:

OR:

NOT:

Conditional operator condition ? true expression:false


expression
int a
float
(a ==
(a ==
int c
Example

= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

FOR loop

The syntax of a for loop in C++ is:


for ( init; condition; increment )
{
statement(s);
}

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

FOR loop: flow diagram

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

FOR loop (cont.)


I

The init step is executed first, and only once. This step allows
you to declare and initialize any loop control variables. You
are not required to put a statement here, as long as a
semicolon appears.
Next, the condition is evaluated. If it is true, the body of the
loop is executed. If it is false, the body of the loop does not
execute and flow of control jumps to the next statement just
after the for loop.
After the body of the for loop executes, the flow of control
jumps back up to the increment statement. This statement
allows you to update any loop control variables. This
statement can be left blank, as long as a semicolon appears
after the condition.
The condition is now evaluated again. If it is true, the loop
executes and the process repeats itself (body of loop, then

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

Example

One statement in body loop

int i;
for (i = 1; i <= 15; i++)
cout << i*i << " ";
cout << endl;

multiple statements in body


loop

for (i=1; i<=15; i++)


{
cout << setw(4) << i << "
int cube = i*i*i;
cout << setw(6) << cube <<
}

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

WHILE loop
A while loop statement repeatedly executes a target statement as
long as a given condition is true.
while(condition)
{
statement(s);
}
I

Usually be used in the case of unknown loop number;

The body of loop is executed as long as the conditional


expression is TRUE;

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

WHILE loop: flow diagram

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

Example

One statement in body loop

int i;
for (i = 1; i <= 15; i++)
cout << i*i << " ";
cout << endl;

multiple statements in body


loop

for (i=1; i<=15; i++)


{
cout << setw(4) << i << "
int cube = i*i*i;
cout << setw(6) << cube <<
}

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

DO loop
A do...while loop is similar to a while loop, except that a do...while
loop is guaranteed to execute at least one time.
do
{
statement(s);
}while(condition);
I

Notice that the conditional expression appears at the end of


the loop

If the condition is true, the flow of control jumps back up to


do, and the statement(s) in the loop execute again. This
process repeats until the given condition becomes false.

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

WHILE loop: flow diagram

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

Example

One statement in body loop

int i;
for (i = 1; i <= 15; i++)
cout << i*i << " ";
cout << endl;

multiple statements in body


loop

for (i=1; i<=15; i++)


{
cout << setw(4) << i << "
int cube = i*i*i;
cout << setw(6) << cube <<
}

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

IF.. THEN
An if statement consists of a boolean expression followed by one or
more statements.

if(boolean_expression)
{
// statement(s) will execute if the boolean expression i
}
else
{
// statement(s) will execute if the boolean expression is
}
I

If the boolean expression evaluates to true, then the if block


of code will be executed, otherwise else block of code will be
executed.

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

IF..THEN: flow diagram

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

IF..THEN: Example

One statement in body loop

int i;
for (i = 1; i <= 15; i++)
cout << i*i << " ";
cout << endl;

multiple statements in body


loop

for (i=1; i<=15; i++)


{
cout << setw(4) << i << "
int cube = i*i*i;
cout << setw(6) << cube <<
}

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

break, continue

break:

continue:

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

Function: structure details

Return Type: A function may return a value. The


return type is the data type of the value the function returns.
Some functions perform the desired operations without
returning a value. In this case, the return type is the keyword
void.

Function Name: This is the actual name of the function.


The function name and the parameter list together constitute
the function signature.

Chapter 2. C++ BASICS


Fundamental statement: loop and conditional statement

Function: structure details

Parameters: A parameter is like a placeholder. When a


function is invoked, you pass a value to the parameter. This
value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a
function may contain no parameters.

Function Body: The function body contains a collection of


statements that define what the function does.

Chapter 2. C++ BASICS


FUNCTIONs

Function: structure, role

Code can be divided into separate functions. Logically the division


usually is so each function performs a specific task.
I

A function declaration tells the compiler about a functions


name, return type, and parameters.

A function definition provides the actual body of the function.

The C++ standard library provides numerous built-in functions


that your program can call. For example, function strcat() to
concatenate two strings, function memcpy() to copy one memory
location to another location and many more functions.

Chapter 2. C++ BASICS


FUNCTIONs

Function: structure, role

Code can be divided into separate functions. Logically the division


usually is so each function performs a specific task.
I

A function declaration tells the compiler about a functions


name, return type, and parameters.

A function definition provides the actual body of the function.

The C++ standard library provides numerous built-in functions


that your program can call. For example, function strcat() to
concatenate two strings, function memcpy() to copy one memory
location to another location and many more functions.

Chapter 2. C++ BASICS


FUNCTIONs

Function: structure, role

Code can be divided into separate functions. Logically the division


usually is so each function performs a specific task.
I

A function declaration tells the compiler about a functions


name, return type, and parameters.

A function definition provides the actual body of the function.

The C++ standard library provides numerous built-in functions


that your program can call. For example, function strcat() to
concatenate two strings, function memcpy() to copy one memory
location to another location and many more functions.

Chapter 2. C++ BASICS


FUNCTIONs

Function: An example
Following is the source code for a function called max(). This
function takes two parameters num1 and num2 and returns the
maximum between the two:
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}

Chapter 2. C++ BASICS


FUNCTIONs

Function: How to call?

When a program calls a function, program control is


transferred to the called function. A called function performs
defined task and when its return statement is executed or
when its function-ending closing brace is reached, it returns
program control back to the main program.

To call a function, you simply need to pass the required


parameters along with function name, and if function returns
a value, then you can store returned value.

Chapter 2. C++ BASICS


FUNCTIONs

Function: An example
Following is the source code for a function called max(). This
function takes two parameters num1 and num2 and returns the
maximum between the two:

#include <iostream>
using namespace std;
int max(int num1, int num2);// function declaration
int main ()
{
int a = 100, b = 200, ret;
// local variable declarat
ret = max(a, b);
// calling a function to get max valu
cout << "Max value is : " << ret << endl;
return 0;
}
int max(int num1, int num2)
{

Chapter 2. C++ BASICS


FUNCTIONs

Function: Arguments

If a function is to use arguments, it must declare variables that


accept the values of the arguments. These variables are called the
formal parameters of the function.

This method copies the actual value of an argument into the formal par

Chapter 2. C++ BASICS


FUNCTIONs

QUESTION and ANSWER

Das könnte Ihnen auch gefallen