Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views8 pages

Functions

The document discusses the concept of functions in programming, highlighting their advantages such as modular programming, code reusability, and reduction in program size. It explains the parts of a function, including declaration, definition, and function calls, as well as the concept of function prototyping and overloaded functions in C++. Additionally, it introduces inline functions, which can improve speed but may increase the size of the executable program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Functions

The document discusses the concept of functions in programming, highlighting their advantages such as modular programming, code reusability, and reduction in program size. It explains the parts of a function, including declaration, definition, and function calls, as well as the concept of function prototyping and overloaded functions in C++. Additionally, it introduces inline functions, which can improve speed but may increase the size of the executable program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

FUNCTIONS

When concept of functions were not introduced the


program were large. The process of dividing a large program into
smaller and handy sub programs is done using functions. This is
similar to divide and conquer method.
ADVANTAGES OF FUNCTIONS :
1. Modular programming is supported
2. Reduction in program size
3. Code duplication is avoided
4. Code reusability is provided
5. Repetitive calls for a function can be done
6. A set of function can be used to form libraries.
PARTS OF FUNCTION:
1.DECLARATION 2.DEFINITION
3.FUNCTION CALL 4.RETURN STATEMENT
5.ACTUAL AND FORMAL PARAMETERS
SYNTAX OF FUNCTIONS
void show( ); //function declaration
void main( )
{
………..
show( ); // function call
………..
}
void show( ) // function definition
{
………..
………..
}
Here void main( ) is the important function from where the
compiler starts execution .Without this function the compiler shows
error.
FUNCTION PROTOTYPING
The prototype describes the function as interface to
the compiler by giving details such as numbers and type of arguments
and the type of return values .

Template function declaration and definition

This prototyping ensures the definition of


proper arguments and return of correct value. If any violation occurs
the compiler comes out of compilation .Function prototype is a
declaration statement in the calling program .
type function_name(argument_list);
here the argument_list contains the type and
name of the arguments that must be passed to the function. Let us see
an example below.
EXAMPLE
Consider the function below
float volume (int x, float y, float z); //function declaration
void main()
{
………..
cube 1 = volume( 10,20.0,30.0); // function call
………..
}
float volume(int a, float b, float c) // function definition
{
float v = a*b*c;
………..
} //here a , b , c are dummy variables used to avoid confusion for the
compiler
PASSING AND RETURNING FUNCTION BY
REFERENCE
PASS RETURN
void swap( ); void max( );
Void main( ) Void main( )
{ {
……. …….
swap( a , b ) max( a , b )
……. …….
} }
swap(int &x ,int &y) max(int &x ,int &y)
//formal parameters bound to {
memory location of actual if (x>y)
parameter return x;
{ else
……. return y;
} }
10 1552(x) Here function returns the
20 1554(Y) reference variables x or y
Overloaded Functions
Unlike regular C, C++ enables you to have more than
one function with the same name. This is called overloading of
functions. These overloaded functions differ in their argument list.
Given below is the example for overloaded functons.

// Overloads two absolute value functions.


#include <iostream.h> // Prototype cout and cin.
#include <iomanip.h> // Prototype setprecision(2).
int abs(int i); // abs() is overloaded twice
float abs(float x); // as shown by these prototypes.
void main()
{
int ians; // To hold return values.
float fans;
int i = -15; // To pass to the two overloaded
float x = -64.53;
ians = abs(i); // C++ calls the integer abs().
cout << “Integer absolute value of -15 is “ << ians << “\n”;
fans = abs(x); // C++ calls the floating-point abs().
cout << “Float absolute value of -64.53 is “ <<setprecision(2) << fans
<< “\n”;
// Notice that you no longer have to keep track of two
// different names. C++ calls the appropriate
// function that matches the parameters.
return;
}
int abs(int i) // Integer absolute value function
{
if (i < 0)
{ return (i * -1); } // Makes positive.
else
{ return (i); } // Already positive.
}
Inline Implementation
Normally, when a function is called, processing literally jumps from the
calling function to the called function .The processor must stash away
information about the current state of the program. It stores this
information in an area of memory known as the stack, which is also
where local variables are created.
An alternative to a normal function call is to define the function with the
keyword inline. In this case, the compiler does not create a real
function. It copies the code from the inline function directly into the
calling function. No jump is made. It is just as if you had written the
statements of the called function right into the calling function.
Note that inline functions can bring a heavy cost. If the function is called
10 times, the inline code is copied into the calling functions each of
those 10 times. The tiny improvement in speed you might achieve is
more than swamped by the increase in size of the executable program.
Anyway for smaller programs it can be used to increase the speed. The
syntax for defining inline function is
inline function_name (parameters);

You might also like