CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)
CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)
CS-323 Programming
Programming Fundamentals
Fundamentals
4(3-2)
4(3-2)
Lecture#
Lecture#13
13
Today’s
Today’s Lecture
Lecture
• Header Files
Programming Fundamentals 2
Header
Header Files
Files
• iostream.h
#include <iostream.h>
• math.h
#include <math.h>
• Examples;
– double sqrt ( double );
– log10 , pow ( xy ) , sin , cos , tan …
• rand ( )
# include <stdlib.h>
Output in the range: 0 – 32767
Programming Fundamentals 3
Header
Header Files
Files -- #define
#define
• Constant values like pi = 3.1415926 can be defined in a header files
#define pi 3.1415926
– Name (pi) can be used inside a program exactly like a variable
• Examples:
Circumference = 2 * pi * radius
Programming Fundamentals 4
Scope
Scope of
of Identifiers
Identifiers (variable)
(variable)
• Identifier is any name user creates in his / her program
– Functions / variables are also identifiers
Programming Fundamentals 5
Scope
Scope and
and Storage
Storage Class
Class
• The scope of a variable determines which parts of the program can
access it. It is the visibility of variables.
– Local Scope: variables with local scope are visible only within a block.
– Global / File scope: variables with file scope are visible throughout a file.
Programming Fundamentals 6
Local
Local variables
variables
• A variable’s scope, also called visibility, describes the locations within
a program from which it can be accessed.
• Local variables are visible within the function body or block
void somefunc()
{
int somevar; //local variables
float othervar; //local variables
somevar = 10; //OK
othervar = 11; //OK
nextvar = 12; //illegal: not visible in somefunc()
}
void otherfunc()
{
int nextvar; //local variable
somevar = 20; //illegal: not visible in otherfunc()
othervar = 21; //illegal: not visible in otherfunc()
nextvar = 22; //OK
}
Programming Fundamentals 7
Local
Local variables
variables -- Automatic
Automatic storage
storage class
class
• Local variables are also called automatic variables, because they
have the automatic storage class.
– Automatic means that the variables are automatically created when a
function is called and automatically destroyed when it returns.
• A local variable is not created until the function in which it is defined is
called.
• The time period between the creation and destruction of a variable is
called its lifetime.
void somefunc()
{
int somevar; //variables defined within…
float othervar; //the function body
…………….. // other statements
}
Programming Fundamentals 8
Local
Local variable
variable -- Automatic
Automatic storage
storage class
class
Programming Fundamentals 9
Global
Global variable
variable
• Global variables are declared/ defined outside any function
• A global variable is visible to all the functions in a file
• It is visible to all those functions that follow the variable’s
definition in the listing
• Global variables are also sometimes called external variables,
since they are defined external to any function.
• For example;
#include <iostream.h>
int i;
Global
variable
Programming Fundamentals 10
Global
Global variables…
variables…
#include <iostream>
using namespace std;
#include <conio.h> //for getch() void getachar() //getachar() accesses ch
{
char ch = 'a'; //exteral variable ch ch = getch();
}
void getachar(); //function declarations //---------------------------------------------------------
void putachar();
Programming Fundamentals 11
Global
Global variables…
variables…
• Example: Global Scope
#include < iostream.h >
Output
int i ;
void f ( void ) ;
main ( ) within main i = 10
{ Inside function f , i = 10
i = 10 ; within main i = 20
cout << “ within main i = “ << i ;
f();
cout << “ within main i = “ << i ;
}
void f ( void )
{
cout << “ Inside function f , i =“ << i ;
i = 20 ;
}
Programming Fundamentals 12
Global
Global variables
variables -- Life
Life time
time and
and visibility
visibility
• Global variables have storage class static, which means they exist for
the life of the program.
• Memory space is set aside for them when the program begins, and
exists till the program ends.
• Global variables are visible in the file in which they are defined, starting
at the point where they are defined.
• It can be used anywhere in program
• It can cause logical problems if same variable name is used in local
variable declarations
Programming Fundamentals 13
Static
Static Local
Local variables
variables
• A static local variable has the visibility of an automatic local
variable (that is, inside the function containing it).
• Its lifetime is the same as that of a global variable, except
that it doesn’t come into existence until the first call to the
function containing it.
• Static local variables are used when it’s necessary for a
function to remember a value when it is not being
executed; that is, between calls to the function.
Programming Fundamentals 14
Static
Static Local
Local variables
variables
#include <iostream> float getavg(float newdata)
using namespace std; {
float getavg(float); //declaration static float total = 0;
static int count = 0;
int main()
{ count++;
float data=1, avg; total += newdata;
return total / count;
while( data != 0 ) }
{
cout << “Enter a number: ”; Output:
cin >> data; Enter a number: 10
avg = getavg(data); New average is 10
cout << “New average is ” << avg << endl; Enter a number: 20
} New average is 15
return 0; Enter a number: 30
} New average is 20
Conclusion
When static variables are initialized, as total and count in getavg(), the initialization takes
place only once – the first time their function is called.
They are not reinitialized on subsequent calls to the function, as ordinary local variables are.
Programming Fundamentals 15
Summary
Summary of
of the
the Visibility
Visibility of
of Identifiers
Identifiers
• Global Scope
Anything identified or declared outside of any function is
visible to all functions in that file
Programming Fundamentals 16
Programming
Programming Assignment
Assignment
1. Fibonacci series
Programming Fundamentals 17
THANK
THANK YOU
YOU
Programming Fundamentals 18