0% found this document useful (0 votes)
12 views23 pages

Functions

The document discusses functions in C/C++ programming. It defines functions as a modular way to divide programs into reusable blocks of code. The main() function acts as the starting point for program execution. Functions have components like a declaration, definition, parameters, return values, and can be called within a program. The document also covers topics like inline functions, function overloading, and storage classes.

Uploaded by

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

Functions

The document discusses functions in C/C++ programming. It defines functions as a modular way to divide programs into reusable blocks of code. The main() function acts as the starting point for program execution. Functions have components like a declaration, definition, parameters, return values, and can be called within a program. The document also covers topics like inline functions, function overloading, and storage classes.

Uploaded by

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

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
}

 int main(int argc, char *argv[])


FUNCTION COMPONENTS
 Function declaration or prototype
 Function formal parameter
 Function definition
 Function body
 Return statement
 Function call
 Elimination of function protocol
 By defining the function before calling it
FUNCTION PROTOTYPING
 The prototype describes the function interface
to the compiler by giving details
 Number and type of arguments.
 The type of return values.
 Declaration statement in the calling program.
Type function-name(argument-list);

 Argument list contains the types and names of


argument that must be passed to the function.
 Either include or exclude the variable names
in the argument list of prototypes.
FUNCTION DEFINITION
 Names are required because the arguments
must be referenced inside the function.
Float volume(int a,float b,float c)
{
float v=a*b*c;
:::::
}

Float cube1=volume(b1,w1,h1);//function call


 Variable b1,w1,h1 –actual parameters
PARAMETER PASSING
 Pass by value
 Pass by address
 Pass by reference
 Hasthe functionality of pass by address and
syntax of pass by value
PARAMETER PASSING BY
REFERENCE
 Argument passing – by value, by pointer or by
reference
 In pass by reference – reference of actual
parameter is passed
void swap(int *p, int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
Call to this function ----
swap(&x,&y);
void swap(int &p, int &q)
{
int t;
t=p;
p=q;
q=t;
}
A call to this function -----
swap(x,y);
RETURN BY REFERENCE
Int & max(int &x,int &y)
{
if (x>y)
return x;
else
return y;
}
Since the return type of max() is int&, the function
returns references to x or y(not the values).
Max(a,b)=-1; assigns -1 to a if it is larger otherwise
-1 to b.
DEFAULT ARGUMENTS
 Call to a function without specifying all its
arguments
 The functions assigns a default value to the
parameter which does not have a matching
argument
 Default values are specified when the
function is declared
 Only trailing arguments can have default
values – ( defaults from right to left)

int mul(int i, int j=5,int k=10)


// function call
mul(2);
mul(3,6);
INLINE FUNCTIONS
 When a function is called  jumping to the
function, saving registers, pushing arguments
into the stack, returning to the calling
function.
 Use macros- In C
 They are not really functions – the usual
error checking does not occur during
compilation
INLINE FUNCTION
 A function that expanded in line when it is invoked.

 Function body is inserted in place of the function call


statement during the compilation process

 Will not incur any context switching overhead

 Advisable to declare the functions having a small


function body as inline function

 Programs with inline function execute faster


inline function_header()
{
function body;
}

inline double cube(double a)


{
return (a*a*a);
}

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

Returntype main(int argc, char *argv[], [char **envp])


{
//body
}

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;

You might also like