Functions
Functions
Modular programming
INTRODUCTION
Dividing a Program into functions
Major principle of top-down approach
Structured programming
It is difficult to isolate errors in large
programs
A repeated group of instruction – function
More reliable and flexible.
THE MAIN FUNCTION
Starting point of execution of a program
int main(){ } -returns a value of type int to OS ( 0
means program runs successfully)
int main()
{
…
….
return 0
}
c = cube(3.0);
d = cube(2.5 + 1.5)
Inline function must be defined before they
are called
Inline keyword merely sends a request, not a
command
Inline expansion may not works
For functions returning values,if a loop,a switch,
or a goto exists.
For functions not returning values, if a return
statement exists.
If functions contain static variables.
If inline functions are recursive.
FUNCTION OVERLOADING
Multiple function to share the same name
Example program
Can give same name provided the signature of each of
them is unique
Functions same name different action
Type signature unique either in the number or data
of their arguments
In source code same name but the compiler
uses different names
Conversion is called as name mangling
C++ does not permit overloading of function
differs only in their return value eg printf
function
STORAGE CLASS
Declaration versus definition
Auto variables
Register variables
Static variables
Extern variables
Recursive functions
COMPLETE SYNTAX OF MAIN()
Array of
Function pointers
Argum pointer to
return to
ents command
type void environme
count line
or int nt
arguments
variables
envp:
-Environment parameter
- holds pointers to environment variables set in the OS
- includes path and environment parameters
- optional and not in ANSI specification
USE OF NEW AND DELETE
#include<iostream.h>
Void main()
{ clrscr();
int * arr;
int size;
cout<<“enter the size of integer array”;
cin>>size;
cout<<“creating an array”
arr=new int[size];
cout<<“dynamic allocation is successfull”;
delete arr;
getch();
}
#include<iostream.h>
int main(int argc, char *arv[], char **envp)
{
cout << “the no fo command line arg are: << argc<< endl;
cout << the command line argument are “ << endl;
for(int i=0;i<argc;i++)
cout << “argv[“ <<i<<“] : “<< argv[i] << endl;
/* cout << environment variables are:” << endl;
int i=0;
while(*envp[i])
cout << envp[i++] << endl;*/
return 0;