0% found this document useful (0 votes)
26 views18 pages

CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)

C++ programming language

Uploaded by

Muhammad Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views18 pages

CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)

C++ programming language

Uploaded by

Muhammad Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

CS-323

CS-323 Programming
Programming Fundamentals
Fundamentals
4(3-2)
4(3-2)
Lecture#
Lecture#13
13
Today’s
Today’s Lecture
Lecture
• Header Files

• Scope & Life time of Variables / Identifier

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

• User Defined Header files


#include “userdefined.h”

Programming Fundamentals 3
Header
Header Files
Files -- #define
#define
• Constant values like pi = 3.1415926 can be defined in a header files

• It is simply to include the header file with a keyword #define followed


by a value in the program, such as:

#define pi 3.1415926
– Name (pi) can be used inside a program exactly like a variable

– It cannot be used as a variable

– It is also called Preprocessor Directive

• Examples:

CircleArea = pi * radius * radius

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

• Identifiers Important Points about Identifier / variables


– Do not create variables with same name inside blocks, inside
functions
– Use separate variable names to avoid confusion

– Reuse of variables is valid (Take Care!!!)

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.

• Storage class determines how long a variable stays in existence. It


determines the life time of a variable.
– Variables with storage class automatic exist during the lifetime of the
function in which they’re defined.
– Variables with storage class static exist for the lifetime of the program.

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

• For example: Block Scope

for (int i = 0 ; i < 10 ; i++)

– It is block level scope declared in for loop

– When for is finished “ i ” no longer exists

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();

int main() void putachar() //putachar() accesses ch


{ {
while( ch != '\r' ) //main() accesses ch cout << ch;
{ }
getachar();
putachar();
}
return 0;
}

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

For good programming

• Try to minimize the use of global variables


• Try to use local variables as possible starting at the point where they
are defined.

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

• Function level scope


Declaring variables inside a function can be used in the
whole function

• Block level scope


Variables or integers declared inside block are used inside
block

Programming Fundamentals 16
Programming
Programming Assignment
Assignment
1. Fibonacci series

2. Find “Power of number” using recursive technique

Programming Fundamentals 17
THANK
THANK YOU
YOU

Programming Fundamentals 18

You might also like