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

Module 9

Uploaded by

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

Module 9

Uploaded by

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

COMPUTER

PROGRAMMING

MODULE 9
FUNCTION IN C++

A function is a group of statements that together perform a specific task. Every C++
program has at least one function, which is main().

Why use function?


Functions are used to divide a large code into module, due to this we can easily debug and
maintain the code. For example, if we write a calculator program at that time we can write
every logic in a separate function (For addition sum(), for subtraction sub()). Any function
can be called many times.

Advantage of Function
 Code Re-usability
 Develop an application in module format.
 Easily to debug the program.
 Code optimization: No need to write lot of code.

Type of Function
There are two type of function in C++ Language. They are:
 Library function or pre-define function.
 User defined function.

Library function
Library functions are those which are predefined in C++ compiler. The implementation part
of pre-defined functions is available in library files that are .lib/.obj files. .lib or .obj files are
contained pre-compiled code. printf(), scanf(), clrscr(), pow() etc. are pre-defined functions.

2
Limitations of Library function
 All predefined function contains limited task only that is for what purpose a function is
designed for same purpose it should be used.
 As a programmer we do not have any controls on predefined function
 In implementation whenever a predefined function is not supporting user requirement
then go for user defined function.

User defined function


These functions are created by programmer according to their requirement. For example,
suppose you want to create a function to add two number then you create a function with
name sum(). This type of function is called user defined function.

DEFINING A FUNCTION
Defining of function is nothing but gives body to a function, that means write logic inside
function body.
Syntax

return_type function_name(parameter)
{
function body;
}

 Return type: A function may return a value. The return_type is the data type of the value
the function returns. Return type parameters and returns statement are optional.
 Function name: Function name is the name of function it is decided by programmer or
you.
 Parameters: This is a value which is pass in function at the time of calling of function. A
parameter is like a placeholder. It is optional.
 Function body: Function body is the collection of statements.

FUNCTION DECLARATIONS
A function declaration is the process of tells the compiler about a function name. The actual
body of the function can be defined separately.
Syntax

return_type function_name(parameter);

Note: At the time of function declaration, it must be terminated with ';'.

CALLING A FUNCTION
When we call any function, control goes to function body and execute entire code. To call
any function, just write name of function and if any parameter is required then pass
parameter.

3
Syntax

function_name();
or
variable=function_name(argument);

Note: At the time of function calling, it must be terminated with ';'.

Example of Function

#include <iostream>
using namespace std;

void sum(); // declaring a function


int a=10, b=20, c;

void main()
{
clrsct();
sum(); // calling function
}

void sum() // defining function


{
c=a+b;
cout<<"Sum: "<<c;
}

Output

Sum: 30

4
INLINE FUNCTION IN C++

Inline Function is powerful concept in C++ programming language. If a function is inline, the
compiler places a copy of the code of that function at each point where the function is called
at compile time.

To make any function inline function just preceded that function with inline keyword.

Why use Inline function?


Whenever we call any function many time then, it take a lot of extra time in execution of
series of instructions such as saving the register, pushing arguments, returning to calling
function. For solve this problem in C++ introduce inline function.

Advantage of Inline Function


The main advantage of inline function is to make the program faster.

Syntax

inline function_name()
{
//function body
}

Example

#include <iostream>
using namespace std;
int show();
int main()
{
show(); // Call it like a normal function
}

inline int show()


{
cout<<"Hello world";
}

Output

Hello word

You might also like