c++day-3
TRANSCRIPT
-
7/31/2019 C++Day-3
1/34
-
7/31/2019 C++Day-3
2/34
Define Storage class
Types of Storage Class
Category of Functions
Type casting
Define Functions & category of Functions
Inline Function
-
7/31/2019 C++Day-3
3/34
cin
cout
getc()putc()
A series of Instructionsthat are to be executed
more than once
-
7/31/2019 C++Day-3
4/34
USER DEFINED FUNCTION :
SYNTAX :
retu_datatype func_name(arguments){
Body of the function
statements;return;
}
call the function from main() :
syntax :
func_name(arguments );
-
7/31/2019 C++Day-3
5/34
#include
Void hai() // function prototype declaration
void hai() //definition
{
Cout
-
7/31/2019 C++Day-3
6/34
NO ARGUMENT NO RETURN VALUES
ARGUMENT BUT NO RETURN VALUES
NO ARGUMENT WITH RETURN VALUES
WITH ARGUMENT WITH RETURN VALUES
(Based on Return values and passing Arguments)
-
7/31/2019 C++Day-3
7/34
#include
#include
void main()
{
void message();
clrscr();cout
-
7/31/2019 C++Day-3
8/34
/* To perform Addition of two numbers
Without Argument and With Return values */
#include
#include
int add(); //function prototype declarationvoid main()
{
int c;
c=add(); /* Return Variable - c */Cout
-
7/31/2019 C++Day-3
9/34
-
7/31/2019 C++Day-3
10/34
#include#includevoid main(){int b,h;float a;
float area(int,int);clrscr();couth;a=area(b,h);cout
-
7/31/2019 C++Day-3
11/34
-
7/31/2019 C++Day-3
12/34
Every C++ variable has a characteristic called its Storage Class.
All variables have datatype and storage classes
Keyword Where it is Declared
Storage Area Default Initial value Lifetime of a variable
-
7/31/2019 C++Day-3
13/34
1. Local or Auto or Internal variable
2. External or Global variable
3. Static variable
4. Register Variable
-
7/31/2019 C++Day-3
14/34
-
7/31/2019 C++Day-3
15/34
Auto variable are always declared
within a function and they are local
to the function in which they are
declared. Hence they are alsonamed as local variables
Keyword : auto
Declaration : Inside the functionStorage Area : Stack
Initial Value : Garbage value
(At the time of compilation
compiler assigns any value)
Lifetime : Upto that function only
Example :
auto int x; (or) int x;
#includevoid function1();void function2();void main()
{int m=1000;function2();cout
-
7/31/2019 C++Day-3
16/34
#include#include
void main()
{
auto int x; //same as int x;clrscr();
cout
-
7/31/2019 C++Day-3
17/34
-
7/31/2019 C++Day-3
18/34
A variable which can be accesswith in a function and outsidethemain function. These variables
are also named as Globalvariables or External variablesKeyword : extern
Declaration : Outside of the
main() functionStorage Area : CPUmemory
Initial Value : Garbage value
(At the time of compilation
compiler assigns any value)
Lifetime : Upto the entire
program
#includeint k;void function1();void function2();void function3();
void main(){k=20;function1();function2();function3();
}void function1() {k=k+10;cout
-
7/31/2019 C++Day-3
19/34
#include#include
//To Access Data or Function Which is External to
that Function
int x=100;
void main()
{
int x=1000;
clrscr();
cout
-
7/31/2019 C++Day-3
20/34
-
7/31/2019 C++Day-3
21/34
This variable static is constant andthe value is continued in all thesteps.
Keyword : static
Declaration : Inside the functionStorage Area : CPU memory
Initial Value : Zero
Lifetime : The value of the variable
persists between different functioncalls.
Example :
static int x;
/* To print the value of x */#includevoid stat();void main()
{int i;for(i=1;i
-
7/31/2019 C++Day-3
22/34
#include#include
void main()
{
static int x;clrscr();
cout
-
7/31/2019 C++Day-3
23/34
/* Example 2 */#include#includevoid incre(); /* Function prototype
declaration */void main(){clrscr();incre();
incre();incre();getch();}
void incre(){static char x=65;cout
-
7/31/2019 C++Day-3
24/34
-
7/31/2019 C++Day-3
25/34
These variables are stored in CPU
registers and hence they can be
accessed faster than the one which is
stored in memory.
Keyword : register
Declaration : Inside the function
Storage Area : CPU - Register
Initial Value : Garbage value(At the time
of compilation compiler assigns anyvalue)
Lifetime : Upto that function only
Example : register int x;
Note :
register double x; register float y;
Registers are usually a 16bit therefore it
cannot hold a float or double data type value
which require 52 & 64 bytes respectively for
storing a value. But the compiler would treat
as automatic variables
#include#includevoid main(){
register int x;clrscr();Cout
-
7/31/2019 C++Day-3
26/34
#include#include
void main()
{
register int i;clrscr();
cout
-
7/31/2019 C++Day-3
27/34
#include
#includevoid main()
{
int a=10,b;
float c=2.3;
clrscr();b=int(a+c);
Cout
-
7/31/2019 C++Day-3
28/34
This is used to convert one data type to another data type. The automatic typeconversions for evaluating an expression are given below -
For example,
-
7/31/2019 C++Day-3
29/34
Inline function
Basically function are used to save the memory space.
When the compiler sees a function it jumps back to
instruction following to the call.It takes extra time to execute .
To avoid this slow down process inline function are
used.
Inline is a function that is expanded in line when it is
invoked. The compiler replaces the function call when it is
invoked
To make a function as inline ,the inline keyword must
be used before function definition
-
7/31/2019 C++Day-3
30/34
An Inline function is a function with a few statements that is
expanded inline when it is invoked
An inline function specifier is actually a request and not a
command to the compiler, hence it may ignore the request and
compile the function as an ordinary function
Automatic inlining is the process of defining a function inside
the class declaration
-
7/31/2019 C++Day-3
31/34
Common Restrictions while using Inline
An inline function must be defined before it is used.
It should not contain any loops
It should not be recursive
It should not contain any static variables
It should not contain a goto or a switch statement
-
7/31/2019 C++Day-3
32/34
Example for inline function#include
inline float cmtom(int cm){
return (float)cm/100;
}
void main()
{
int cm;
clrscr();
cout
-
7/31/2019 C++Day-3
33/34
// Demonstrate INLINE Functions
#include
float convert(float);
inline float convert(float pounds)
{
float c;
c=0.453592 * pounds;
return(c);
}void main()
{
float w,res;
cout
-
7/31/2019 C++Day-3
34/34
An Inline function is a function with a few statements that is
expanded inline when it is invoked
An inline function specifier is actually a request and not a
command to the compiler, hence it may ignore the request and
compile the function as an ordinary function