You are on page 1of 31

Introduction to C++ programming

Shaobai Kan
chapter 5 (Continue...)

Introduction to C++ programming – p. 1/27


Outline

Introduction to C++ programming – p. 2/27


Outline
• Scope rules

Introduction to C++ programming – p. 2/27


Outline
• Scope rules

• Function call stack and inline function

Introduction to C++ programming – p. 2/27


Outline
• Scope rules

• Function call stack and inline function

• Reference and reference parameters

Introduction to C++ programming – p. 2/27


Scope Rules

Introduction to C++ programming – p. 3/27


Scope of variables

Scope of variables. According to the scope of variables,


there are two types of variables:

• Local variables.
— identifier declared inside a block has block scope.

• Global variables.
— identifier declared outside any function or class has
file scope.

Introduction to C++ programming – p. 4/27


Example 1: local variables

# include <iostream>
using namespace std;
double square ( double );

int main( )
{
double i ;
Scope of variable i = 0.4 ;
i cout << square ( i ) << endl ;

return 0;
}

double square ( double x )


{
double z ;
Scope of variable
x z=x*x; Scope of variable
z
return z;
}

Introduction to C++ programming – p. 5/27


Analysis of example 1

Local variables. Both i and x, z are local variables; they


have different scope.
• i: is inside a function (main) block.

• x, z: are inside a function (square) block.

Fact. Block scope begins at identifier’s declaration and


ends at the terminating brace.

Introduction to C++ programming – p. 6/27


Example 2: local variables

correct incorrect

# include <iostream> # include <iostream>


using std::cout; using std::cout;
using std::endl; using std::endl;

int main( ) int main( )


{ {

int i ;
for ( int i = 1; i < 5 ; i ++)
cout << i << endl;
for ( i = 1; i < 5 ; i ++)
cout << i << endl;

cout << i << endl;


cout << i << endl ;
return 0 ; return 0 ;
} }

Introduction to C++ programming – p. 7/27


Analysis of example 2

Local variables. Both i are local variables; but


they have different block scope in those two
cases.

• 1st case: i is a local variable inside a function


(main) block.

• 2nd case. i is a local variable inside a for


looping block.

Introduction to C++ programming – p. 8/27


Example 3: global variables

# include <iostream>
using namespace std;
int GetValue ( void );

int i = 2; // global variable

int main( )
{
cout << i << endl ;
Scope of variable cout << GetValue ( ) << endl ;
i
return 0;
}

int GetValue ( void )


{
return i * i;
}

Introduction to C++ programming – p. 9/27


Analysis of example 3

Global variables. variable i is a global variable and has


file scope.

• global variable is "known" in all functions from the point


at which it is declared until the end of the file.

• try to avoid the use of global variables if they are not


necessary.

Introduction to C++ programming – p. 10/27


Function call stack and inline function

Introduction to C++ programming – p. 11/27


Stack

Function stack. In computer science, a stack is an


abstract data type and data structure based on the
principle of Last In First Out (LIFO).

stack
last in first out

LIFO ( Last-in first-out)

Introduction to C++ programming – p. 12/27


Function call stack

Function call stack. When your program calls a


function, a "stack frame" is established.

1. The return address of the function is put on


the stack. (When your function returns, it will
resume executing at this address.)
2. Room is made on the stack for the return type
you have declared.
3. All the arguments to the function are placed
on the stack.
Introduction to C++ programming – p. 13/27
Function call stack

4. The program branches to your function.


5. Local variables are pushed onto the stack as
they are defined.

The reverse will take place as the function


completes.

Introduction to C++ programming – p. 14/27


Example

# include <iostream>
using namespace std;
int Square ( int );

int main( )
{
for (int i =1; i <= 5; i++)
cout << Square(i) << endl ;
Function call
5 times return 0;
}
int Square ( int x)
{
int z;
z = x* x;
return z;
}

Introduction to C++ programming – p. 15/27


Inline function

# include <iostream>
using namespace std;

inline function inline int Square ( int x)


{
int z;
z = x* x;
return z;
}
int main( )
{
for (int i =1; i <= 5; i++)
cout << Square(i) << endl ;

return 0;
}

Introduction to C++ programming – p. 16/27


Rules of using inline or not

Rules.

1. When the function body is not too big and the function
call does happen frequently, we can try "inline".

2. When the function body is big and the function call


does not happen so frequently, we should avoid using
"inline".

Introduction to C++ programming – p. 17/27


Reference and Reference parameters

Introduction to C++ programming – p. 18/27


Algorithm: swap values

x y

4 7

Algorithm ?
x y

7 4

Introduction to C++ programming – p. 19/27


Example: swap values

Example

# include <iostream>
using namespace std;

int main( )
{

int x = 4, y = 7;
int t ;

t=x;
algorithm x=y;
y=t;

cout << “x: ” << x << “\ty=” << y << endl;

return 0 ;
}

Introduction to C++ programming – p. 20/27


Swap function

Incorrect code

# include <iostream>
using namespace std;
void Swap( int x, int y );

int main( )
{
int x = 4, y = 7 ;

Pass by values Swap ( x, y ) ;

cout << “x: ” << x << “\ty: ” << y << endl ;


return 0 ;
}

void Swap( int x, int y );


{
int t;
t = x;
x = y;
y = t;
}

Introduction to C++ programming – p. 21/27


Passing arguments by value and by reference

Passing arguments.

• Pass-by-value. a copy of the arguments’value is made


and passed to the called function.

Introduction to C++ programming – p. 22/27


Passing arguments by value and by reference

Passing arguments.

• Pass-by-value. a copy of the arguments’value is made


and passed to the called function.

• Pass-by-reference. the caller gives the called function


the ability to access the caller’s data directly, and to
modify the data if the called function chooses to do so.
— using reference
— using pointers

Introduction to C++ programming – p. 22/27


Using reference parameters

Correct code

# include <iostream>
using namespace std;
void Swap( int &x, int &y );

int main( ) reference operator


{
int x = 4, y = 7 ;
Pass by Swap ( x, y ) ;
reference
cout << “x: ” << x << “\ty: ” << y << endl ;
return 0 ;
}

void Swap( int &rx, int &ry );


{
int t;
t = rx;
rx = ry;
ry = t;
}

Introduction to C++ programming – p. 23/27


Analysis

Pass-by-reference.

• & - reference operator; using them in the function


prototype and header means passing argument by
reference.

• In the definition of the swap function, rx and ry are just


aliases to variables x and y respectively.

Introduction to C++ programming – p. 24/27


Example: alias

Alias

# include <iostream>
using namespace std;

int main( )
{
int intOne ;
int &rintOne = intOne ; //create and alias rintOne

intOne = 5;
cout << “intOne: ” << intOne << endl ;
cout << “rintOne: ” << rintOne << endl ;

rintOne = 7;
cout << “intOne: ” << intOne << endl ;
cout << “rintOne: ” << rintOne << endl ;
return 0;
}

Introduction to C++ programming – p. 25/27


Watch out!!!

Incorrect code

# include <iostream>
using namespace std;

int main( )
{
int x = 3 ;
bug int &y ; // Error: y must be initialized
y=x;

cout << “x = ” << x << endl << “y = ” << y << endl ;

y = 7;
cout << “x = ” << x << endl << “y = ” << y << endl ;

return 0;
}

Introduction to C++ programming – p. 26/27


Homework:
• Read Sec. 5.9 - 5.18.

Introduction to C++ programming – p. 27/27

You might also like