0% found this document useful (0 votes)
13 views

Block and Scope - Tinh

Uploaded by

phamnam28n
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Block and Scope - Tinh

Uploaded by

phamnam28n
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Block and scope

Dr. Nguyen Van Tinh.


Block
• The block consists of paired braces and the statements they enclose.
Block
Scope (phạm vi)
• The scope of a variable is the area in which its name is used. Particularly,
• If the local variables are declared at the beginning of a block, their
scope is the block’s entire body.
• The local variables declared in different blocks are unrelated even if
they have the same name.
Scope of a variable
• If you define a variable
inside a block, the
variable’s scope is limited
to that block.
• If you define a new
variable inside a block,
the variable exists only as
long as the program is
executing statements
within the block.
When execution leaves the block, the
variable is deallocated.
That means the variable is known only
within the block.

Wrong
What happened?
Explanation
• What happens if you declare a variable in a block that has
the same name as the one outside the block?
• Answer
 The new variable hides the old one from its point of
appearance until the end of the block.
 Then the old one becomes visible again when the
program runs to the end of the block.
The purpose of using functions
• The purpose of using functions is
to break a large task into smaller
ones.
• This allows a developer to build on
what has already been finished
instead of starting over from
scratch (do it again many times
from the initial point).
Create your own functions
• when you create your own functions, you have to handle all three
aspects:
• Provide a function definition.
• Provide a function prototype
• Call the function.
What is prototype? (Nguyên mẫu là gì?)
prototype is the first design of something from which other forms are
copied or developed.
What is prototype?
A prototype describes the functional interface to the compiler. That is, it tells
the compiler: what type of return value, if any, and the number and type of
function arguments.

arguments
The purpose of using functions
Header of functions
Function simple() has its function header:
void simple()
The initial void means that simple() has no return value. Thus, calling simple()
doesn’t produce a number that you can assign to a variable in main().
int main()
{
simple(); // ok for void functions
int a;
a = simple(); // Wrong. Not allowed for void functions
}
Definition of a function
Functions into two categories: those that don’t have return values and
those that do.
Functions without return values are termed type void functions and have
the following general form:
void functionName(parameterList)
{
statement(s);
}
Here parameterList specifies the types and number of arguments
(parameters) passed to the function.
Definition of a function
Definition of a function
A function with a return value produces a value. Such a function is declared as having
the same type as the value it returns.

typeName functionName(parameterList)
{
statements;
return value; // value is type cast to type typeName
}

A function terminates after executing a return statement.


Definition of a function
If a function has more than one return statement.
For example, as alternatives to different if else selections, the function
terminates after it executes the first return statement it reaches.
What is the prototype used for?

Prototypes ensure the following tasks:


• The compiler correctly handles the function return value.
• The compiler checks that you use the correct number of function
arguments.
• The compiler checks that you use the correct type of arguments. If you
don’t, it converts the arguments to the correct type, if possible.
Prototype Syntax
• A function prototype is a statement, so it must have a terminating semicolon.
• The simplest way to get a prototype is to copy the function header from the
function definition and add a semicolon.
• However, the function prototype does not require names for the variables; a
list of types is enough.
Function Arguments and Passing by Value
• C language normally passes arguments by value. To be specific, the
numeric value of the argument is passed to the function, where it is
assigned to a new variable. Consider a function having a header as follows:
float cube( float x)
//Definition of cube(float x)
float cube(float x)
{
return x*x*x;
}
Passing by Value //Definition of cube(float x)
int main() float cube(float x)
{ {
float b = 5.0; return x*x*x;
float a = cube(b); }
}
When this function is called, it creates a new float variable called x and
initializes it with the value 5.0.
To put it simply, cube(b) works with a copy of b instead of the original
data of b.
For this reason, the data in main() are protected against actions
happening in function cube(x).
Automatic local variables
Automatic local variables
automatic local variables

• Automatic local variables declared inside a function definition without


keyword static.
• They are created when program execution enters the function or
block in which they are defined.
• the memory used for them is freed when execution leaves the
function or block.
Automatic Local Variables and the Stack
• Because the number of automatic local variables grows and reduces as
functions start and terminate, the program has to manage automatic local
variables as it runs.
• The usual means is to spend a section of memory and treat it as a stack for
managing the automatic variables.
• It’s called a stack because new data is stacked on top of old data and then
removed from the stack when a program is finished with it.
• When a function is called, its automatic local variables are added to the
stack.
• A stack is a LIFO (last-in, first-out) design, meaning the last variables added
to the stack are the first to go.
Static local Variables - keyword static

• C provides static variables that have a longer lifetime than local


(automatic) variables.
• Because the number of static variables doesn’t change as the program
runs, the program doesn’t need a stack to manage them.
• Instead, the compiler allocates a fixed block of memory to hold all the
static variables, and those variables stay there as long as the program
executes.
• if you don’t explicitly initialize a static variable, the compiler sets it to 0.
Static arrays and structures have all the bits of each element
or member set to 0 by default.
Variable count is deleted when strcount( const char*) function is finished,
while total, in contrast, retains (giữ lại) its value and remains in memory
even when strcount( const char*) function is not being executed.

• total still exists even while strcount( const


char*) function is inactive because it is
static.
• If you initialize a static variable, the program
initializes the variable once at start-up time.
Subsequent calls don’t reinitialize it as
automatic one.
Comparison
Automatic variable Static variable
Declaration: Declaration:
int my_value; static int my_value;
float my_value; static float my_value;
allocated a stack to hold value allocated a fixed block of memory to hold
value
It is deleted when the function containing it It still retains its value and remains in
is finished. memory even when the function is not
being executed.
The number of automatic variables can the number of static variables doesn’t
increase or reduce when th program runs. change as the program runs.
Thank you so much.

You might also like