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

Modularity Using Functions Part 2

The document discusses variable scope, storage classes, and pass by reference in functions. It covers local and global variable scopes, the auto, static, extern and register storage classes, and how to pass addresses of variables to functions using pointers and the address and indirection operators.

Uploaded by

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

Modularity Using Functions Part 2

The document discusses variable scope, storage classes, and pass by reference in functions. It covers local and global variable scopes, the auto, static, extern and register storage classes, and how to pass addresses of variables to functions using pointers and the address and indirection operators.

Uploaded by

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

MODUL ARIT Y

USING FUNCTIONS:
PART II
G E R RY R I T Z R . D E N S I N G
Adapted from A First Book of ANSI C, Fourth Edition CC 112 / CC 112L
TOPICS COVERED

• Variable Scope
• Variable Storage Class
• Pass by Reference
• Recursion

2
VARIABLE
SCOPE
3
• Now that we have begun to write programs containing more than one function, we can look
more closely at the variables declared within each function and their relationship to variables
in other functions.
• This term refers to the scope of a variable, where scope is defined as the section of the
program where the variable is valid or “known”.
• The scope of a variable is determined solely by the placement of the declaration statement
that reserves storage for it.

4
LOCAL VARIABLES

• Variables that are only meaningful when used in expressions or statements inside the function
that declares them.
• This means that the same variable name can be declared and used in more than one function.
• All the variables we have used until now have been local variables.

5
GLOBAL VARIABLES

• A variable with global scope, more commonly termed a global variable, is one whose storage
has been created for it by a declaration statement located outside any function.
• These variables can be used by all functions in a program that are physically placed after the
global variable declaration.

6
7
VARIABLE
STORAGE
CL ASS
8
• The scope of a variable defines the location within a program where that variable can be used.
• From this viewpoint, the scope of a variable can be thought of as the space within the program
where the variable is valid.
• The four available storage classes are called auto, static, extern, and register.

9
• If one of these class names is used, it must be placed before the variable’s data type in a
declaration statement.
• To understand what the storage class of a variable means, we will first consider local variables
(those variables created inside a function) and then global variables (those variables created
outside a function).

10
LOCAL VARIABLE STORAGE CLASSES

• Local variables can only be members of the auto, static or register storage classes.
• If no class description is included in the declaration statement, the variable is automatically
assigned to the auto class.
• Thus, auto is the default class used by C.
• All the local variables we have used, since the storage class designation was omitted, have been
auto variables.

11
• As long as the function has not returned control to its calling function, all automatic variables
local to the function are “alive”-----that is , storage for the variables is available.
• When the function returns control to its calling function, its local automatic variables “die”--
that is, the storage of the variables is released back to the operating system.
• This process repeats itself each time a function is called.

12
Output is:
The value of the automatic variable num is 0
The value of the automatic variable num is 0
The value of the automatic variable num is 0

13
• There are cases, however, where we want a function to remember values between function
calls.
• This is the purpose of the static storage class.
• A local variable that is declared as static causes the program to keep the variable and its latest
value even when the function that declared it is through executing.

14
• Examples of static variable declarations are
• static int rate;
• static float taxes;
• static double amount;
• static char inKey;
• static long years;

15
• A local static variable is not created and destroyed each time the function declaring the static
variable is called.
• Once created, local static variables remain in existence for the life of the program.
• This means that the last value stored in the variable when the function is finished executing is
available to the function the next time it is called.

16
• The initialization of static variables (both local and global ) is done only once, when the
program is first compiled.
• At compilation time, the variable is created and any initialization value is placed in it.
• Thereafter, the value in the variable is kept without further initialization each time the function
is called.

17
Output is:
The value of the static variable num is now 0
The value of the static variable num is now 1
The value of the static variable num is now 2

18
• The remaining storage class available to local variables, the register class, is not used as
extensively as either the automatic or static variable classes.
• Example of register variable declarations
– register int m;
• Register variables are similar to auto variables. The only difference is register variables are
stored in CPU register instead of memory.

19
GLOBAL VARIABLE STORAGE CLASSES

• Global variables are created by declaration statements external to a function.


• By their nature, these externally defined variables do not come and go with the calling of any
function.
• Once an external (global) variable is created, it exists until the program in which it is declared
is finished executing.

20
• Examples of declaration statements including these two class descriptions are
• extern int num;
• extern double price;
• static double yield;

21
• The purpose of the extern storage class is to extend the scope of a global variable beyond its
normal boundaries.
• Larger programs typically consist of many functions that stored in multiple files.

22
Output

23
Output

24
• The last global class, static, is used to prevent the extension of a global variable into a second
file.
• Global static variables are declared in the same way as local static variables, except that the
declaration statement is placed outside any function.
• The scope of a global static variable cannot be extended beyond the file in which it is declared.
• Static global variables cannot be subsequently extended to a second file using an extern
declaration statement. Trying to do so results in a compilation error.

25
PASS BY
REFERENCE
26
• In the normal course of operation, a called function receives values from its calling function,
stores the passed values in its own local parameters, manipulates these parameters
appropriately, and possibly returns a single value.
• This method of calling a function and passing values to it is referred to as a function pass by
value.

27
PASSING ADDRESSES TO A FUNCTION

• To pass, store, and use addresses requires the use of the address operator(&), pointers, and the
indirection operator(*).
• Passing addresses to a function should be familiar to you, because we have been using
addresses each time we called the scanf ( ) function.

28
• Output is:
num = 22
The address of num is 124484

29
STORING ADDRESS

• We can also store addresses in suitably declared variables.


• For example, the statement: numAddr = #
• stores the address corresponding to the variable num in the variable numAddr
• Similarly, the statements
• d=&m; tabPoint=&list; chrPoint=&ch;
• store the addresses of the variables m, list, and ch in the variables d, tabPoint, and chrPoint
respectively.

30
POINTER VARIABLES

• The variables numAddr , d, tabPoint, and chrPoint are all called pointer variables, or pointers, for
short.
• Pointers are simply variables that are used to store the addresses of other variables.

31
USING ADDRESSES

• To use a stored address, C provides us with an indirection operator, *(asterisk).


• The * symbol, when followed immediately by a pointer ( no space allowed between the * and
the pointer), means the variable whose address is stored in.
• Thus, if numAddr is a pointer (remember that a pointer is a variable that contains an address),
*numAddr means the variable whose address is stored in numAddr.

32
• Similarly, *tabPoint means the variable whose address is stored in tabPoint, and *chrPoint means
the variable whose address is stored in chrPoint.
• Although *d literally means the variable whose address is stored in d, this is commonly shortened
to the variable pointed to by d.

33
DECLARING AND USING POINTERS

• Like all variables, pointers must be declared before they can be used.
• In declaring a pointer variable, C requires that we also specify the type of variable that is
pointed to.
• For example, if the address in the pointer numAddr is the address of an integer, the correct
declaration for the pointer is
• int *numAddr;

34
• The declaration is read as the variable pointed to by numAddr (from the *numAddr in the
declaration) is an integer.
• Notice that the declaration
• int *numAddr;
• specifies two things: first, that the variable pointed to by numAddr is an integer, second, that
numAddr must be a pointer (because it is used with the indirection operator *).

35
• Similarly, if the pointer tabPoint points to (contains the address of ) a floating point number and
chrPoint points to a character variable, the required declarations are
• float *tabPoint;
• char *chrPoint;
• These two declarations can be read as the variable pointed to by tabPoint is a float and the
variable pointed to by chrPoint is a character.

36
Output is:
The address stored in milesAddr is 1244872
The value pointed to by milesAddr is 22

The value in miles is now 158

37
PASSING ADDRESSES TO A FUNCTION

38
• Sample run of Program 7.6:
Enter a number: 24.6
The address that will be passed is 124484

The address received is 124484


The value pointed to by xnum is: 24.60

39
Add 20.2 to the value of the variable pointed to by xnum

40
RECURSION
41
• Functions that call themselves are referred to as self-referential or recursive functions
• When a function invokes itself, the process is called direct recursion
• A function can invoke a second function, which in turn invokes the first function; this type of
recursion is referred to as indirect or mutual recursion

42
MATHEMATICAL RECURSION

• The definition for n! can be summarized by the following statements:


– 0! = 1
– n! = n * (n-1)! for n >= 1
• This definition illustrates the general considerations that must be specified in constructing a
recursive algorithm:
1. What is the first case or cases?
2. How is the nth case related to the (n-1) case?

43
• In pseudocode, the processing required is
If n = 0
factorial = 1
Else
Factorial = n * factorial(n - 1)
• In C, this can be written as
int factorial(int n)
{
if (n == 0)
return (1);
else
return (n * factorial(n-1));
}

44
45
46
47
48
RECURSION VERSUS ITERATION

• The recursive method can be applied to any problem in which the solution is represented in
terms of solutions to simpler versions of the same problem
• Any recursive function can be written in a nonrecursive manner using an iterative solution
int factorial(int n)
{
int fact;
for(fact = 1; n > 0; n--)
fact = fact * n;
return (fact);
}

49

You might also like