This document discusses functions in C++. It defines a function as a self-contained block of code that performs a specific task. Functions have a declaration, definition, and call. There are library functions provided in header files and user-defined functions. The declaration specifies the name, return type, and parameters. The definition contains the body of the function. Functions are called by name with arguments. Functions can be local or global in scope. Function overloading allows multiple functions with the same name but different parameters. Inline functions have their code inserted at the call site for faster execution.
2. What is the function?
Definition: function is self-contained
block of statement that perform a
specific task.
Function component:
F. declaration
F. definition
F. call
3. Types of function
Function is divided into two parts:
Library functions:- are the parts of header files.
sum(), strcpy(), get().
User defined :- are provided by
user(programmer).
main().
function
Library
function
User defined
function
4. Function declaration
The declaration tells to the computer the
name, return type, parameters of the function.
The statement is placed after
#include<iostream.h> and before the main()
method.
syntax:
return-type function-name();
or
return-type function-name(argument-list);
example: int sum(int n);
5. Function definition
Function definition tells what task the
function will be performing.
difference between F. definition and F.
declartion is a semicolon ”;” .
Placed aftre the main().
HEADER// int heading(void)
{
BODY // statement;
}
6. Function call
A function only gets life only when it is
called.
Function call specified by the function
name followed by the arguments
terminated by a semicolon.
Syntax: <function-name>(argument list); or
variable-name= <function-name>(argument list);
Example: n=avg(12,6);
7. Scope of a function
Local function:
declared inside another function.
can be accessed only inside that function.
Global function:
declared outside the main() function.
Can be accessed anywhere in the program.
8. Function over loading
It is the process of using same name for
two or more function.
Condition is :should use different types of
parameters.
9. Inline function
A function whose code is copied in place of each function
call.
Declared by prefixing inline keyword to F.call and
F.definition.
Syntax: inline<function-name>()
{
Body of the function
}
Example: Inline int add(int x,int y)
{
Return x+y;
}
10. Advantages and
disadvantages of inline
Advantages:
1. function calling
not required.
2. executed
faster than
normal function.
Disadvantages:
1. if inline function
are too large
program goes
larger.
2. it makes header
file size larger,may
make it
unreadable.
11. Program for function
explanation
#include<iostream.h>
Inline double avg(double x, double y); //function declaration.
Int main()
{
Souble a,b;
Cout<<“enter first number:”;
Cin>>a;
Cout<<“enter the second number:”;
Cin>>b;
Cout<<“average is:”<<avg(a,b);// function called.
Return 0;
Inline double avg(double x, double y) // function definition.
{
Return (x+y)/2;
}