Professional Documents
Culture Documents
putc()
USER DEFINED FUNCTION : SYNTAX : retu_datatype func_name(arguments) { Body of the function statements; return; } call the function from main() : syntax : func_name(arguments );
#include<iostream.h> Void hai() // function prototype declaration void hai() //definition { Cout<<" Welcome to functions\n"; Cout<<"Good Morning\n"; } void main() { Cout<<"Main, Welcome to functions\n"; hai(); //calling Cout<<"Bye"; }
Without Argument and With Return values */ #include<iostream.h> #include<conio.h> int add(); //function prototype declaration void main() { int c; c=add(); /* Return Variable - c */ Cout<<"The sum of two numbers is <<c; } int add() { int a,b,c; Cout<<"Enter two Numbers="; Cin>>a>>b; c=a+b; return(c);
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
1. Local or Auto or Internal variable 2. External or Global variable 3. Static variable 4. Register Variable
Auto variable are always declared #include<iostream.h> void function1(); within a function and they are local void function2(); to the function in which they are void main() declared. Hence they are also { int m=1000; named as local variables function2(); cout<<m; Keyword : auto } Declaration : Inside the function void function1() Storage Area : Stack { Initial Value : Garbage value int m=10; (At the time of compilation cout<<m; } compiler assigns any value)
#include<iostream.h> #include<conio.h> void main() { auto int x; //same as int x; clrscr(); cout<<endl<<x; getch(); }
A variable which can be access with in a function and outside the main function. These variables are also named as Global variables or External variables Keyword : extern Declaration : Outside of the main() function
#include<iostream.h> int k; void function1(); void function2(); void function3(); void main() { k=20; function1(); function2(); function3(); } void function1() { k=k+10; cout<<k; } void function2() { k=k+1000; cout<<k; } void function3() { k=k+10; cout<<k; }
#include<iostream.h> #include<conio.h> //To Access Data or Function Which is External to that Function int x=100; void main() { int x=1000; clrscr(); cout<<endl<<"Local Data"<<x; cout<<endl<<"Global Data"<<::x; getch(); }
This variable static is constant and the value is continued in all the steps. Keyword : static
/* To print the value of x */ #include<iostream.h> void stat(); void main() { Declaration : Inside the function int i; Storage Area : CPU memory for(i=1;i<=5;i++) stat(); //calling Initial Value : Zero Lifetime : The value of the variable } void stat() //definition persists between different function { calls. static int x=0; cout<<x; Example : x=x+1; static int x; }
0 1 2 3 4 5
/* Example 2 */ #include<iostream.h> #include<conio.h> void incre(); /* Function prototype declaration */ void main() { clrscr(); incre(); incre(); incre(); getch(); } void incre() { static char x=65; cout<<x++; }
These variables are stored in CPU #include<stdio.h> registers and hence they can be #include<conio.h> accessed faster than the one which is void main() stored in memory.
{ register int x; Keyword : register clrscr(); Declaration : Inside the function Cout<<x; getch(); Storage Area : CPU - Register Initial Value : Garbage value(At the time }
of compilation compiler assigns any value) 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<iostream.h> #include<conio.h> void main() { register int i; clrscr(); cout<<"The I value is "<<i; for(i=1;i<=5;i++) { cout<<endl<<i; } getch(); }
#include<iostream.h> #include<conio.h> void main() { int a=10,b; float c=2.3; clrscr(); b=int(a+c); Cout<<"The value of b is <<b; getch(); }
Type Conversion
This is used to convert one data type to another data type. The automatic type conversions for evaluating an expression are given below -
For example,
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
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
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
// Demonstrate INLINE Functions #include<iostream.h> float convert(float); inline float convert(float pounds) { float c; c=0.453592 * pounds; return(c); } void main() { float w,res; cout<<endl<<"Enter Your Weights in Pounds :"; cin>>w; res=convert(w); cout<<"Your Weight in Kilograms is "<<res<<endl; }