Sunteți pe pagina 1din 34

Session Objectives

Define Functions & category of Functions


Define Storage class

Types of Storage Class


Category of Functions Type casting Inline Function

cin cout getc()

putc()

A series of Instructions that are to be executed more than once

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"; }

(Based on Return values and passing Arguments)

NO ARGUMENT NO RETURN VALUES

ARGUMENT BUT NO RETURN VALUES

NO ARGUMENT WITH RETURN VALUES


WITH ARGUMENT WITH RETURN VALUES

NO ARGUMENT NO RETURN VALUES


#include<iostream.h> #include<conio.h> void main() { void message(); clrscr(); cout<<endl;"Welcome to C++"; message(); cout<<endl<<"end"; getch(); } void message() { cout<<endl<<"Welcome to the Function"; }

/* To perform Addition of two numbers

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

WITH ARGUMENT NO RETURN VALUES


#include<iostream.h> void main() { int b,h; void area(int,int); cout<<endl<<"Enter Base and Height Values"; cin>>b>>h; area(b,h); cout<<endl<<"end"; } void area(int x,int y) { float at; at=0.5*x*y; cout<<"area is :"<<at; }

WITH ARGUMENT WITH RETURN VALUES


#include<iostream.h> #include<conio.h> void main() { int b,h; float a; float area(int,int); clrscr(); cout<<endl<<"Enter Base and Height Values"; cin>>b>>h; a=area(b,h); cout<<endl<<"Area of the Triangle is "<<a; getch(); } float area(int x,int y) { float at; at=0.5*x*y; return at; }

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)

Lifetime : Upto that function only


Example : auto int x; (or) int x;

void function2() { int m=100; function1();


cout<<m;

#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

Storage Area : CPUmemory


Initial Value : Garbage value (At the time of compilation compiler assigns any value) Lifetime program : Upto the entire

#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

#include<iostream.h> #include<conio.h> void main() { static int x; clrscr(); cout<<endl<<x; getch(); }


Zero (0)

/* 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++; }

The character Stored in x is A The character Stored in x is B The character Stored in x is C

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

-899 (Garbage Value)

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

Used to convert one datatype into another data type

#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

compile the function as an ordinary function


Automatic inlining is the process of defining a function inside

the class declaration

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

Example for inline function


#include<iostream.h> inline float cmtom(int cm) { return (float)cm/100; } void main() { int cm; clrscr(); cout<<"enter the length in centimeter:"<<endl; cin>>cm; cout<<"the equivalent meter value is :"<<cmtom(cm)<<endl; }

// 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; }

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

S-ar putea să vă placă și