0% found this document useful (0 votes)
74 views7 pages

CP II Unit IV Part II

The document discusses various aspects of functions in C++ including function prototyping, definition, call by reference, return by reference, inline functions, default arguments, and more. Function prototyping involves declaring a function's interface including number/type of arguments and return type. A function must be defined with a body before use. C++ allows passing arguments by reference so changes are reflected in the calling function. Functions can also return references. Inline functions avoid overhead of regular function calls. Default arguments allow omitting trailing arguments if defaults are specified in the declaration.

Uploaded by

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

CP II Unit IV Part II

The document discusses various aspects of functions in C++ including function prototyping, definition, call by reference, return by reference, inline functions, default arguments, and more. Function prototyping involves declaring a function's interface including number/type of arguments and return type. A function must be defined with a body before use. C++ allows passing arguments by reference so changes are reflected in the calling function. Functions can also return references. Inline functions avoid overhead of regular function calls. Default arguments allow omitting trailing arguments if defaults are specified in the declaration.

Uploaded by

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

Reference: - Object Oriented Programming With C++ by E.

Balagurusamy

User Define Functions in C++


- Function Prototyping - Constant Arguments
- Call by Reference - Default Arguments
- Return by Reference - Function Overloading
- Inline Functions - Friend and Virtual Functions

Function Prototyping (Declaration).

- The prototype describes the function interface to the compiler by giving details such
as
o Number of arguments
o Type of arguments
o Type of return value
- Function prototype is a declaration statement in the calling program and is of the
following form:
- Syntax
Return_type fuction_name( argument-list);
o The argument-list contains the types and names of arguments that must be
passed to the function.
- Example
float volume (int x, float y, float z);

o Note that each argument variable must be declared independently in side


parenthesis.
o In a function declaration, the names of the arguments are dummy variables
and therefore they are optional. That is the form,
float volume(int, foat, float);
o At this stage, the compiler only checks for the type of arguments when the
function is called.
- In general, we can either include or exclude the variable names in the arguments list
of prototypes.
- The variable names in prototype just act as placeholders and therefore, if names are
used, they don’t have to match the names used in function call or function definition.

Function Definition

- In C++, a function must be defined prior to its use in the program.


- The function definition contains the code for the function.
- The general syntax of a function definition in C++ is shown below:
- Syntax: function Definition.
Type name_of_the_function (argument list) {

Computer Programming II Notes Prepared By: - Dhananjay Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

//body of the function


}
o Here, the type specifies the type of the value to be returned by the function.
o It may be any valid C++ data type.
o Name_of_the_function is a valid C++ identifier (no reserved word allowed)
defined by the user and it can be used by other functions for calling this
function.
o Argument list is a comma separated list of variables of a function through
which the function may receive data or send data when called from other
function.
o When no parameters,the argument list is empty.
- Example:- illustrates the concept of function definition :

- void add( ) {
int a,b,sum;
cout<<”Enter two integers”<<endl;
cin>>a>>b;
sum=a+b;
cout<<”\nThe sum of two numbers is “<<sum<<endl;
}
- The above function add ( ) can also be coded with the help of arguments of
parameters as shown below:

void add(int a, int b) //variable names are must in definition


{
int sum=a+b;
cout<<”\nThe sum of two numbers is “<<sum<<endl;
}

- function definition for volume function declared in above part:

float volume( int a, float b, float c){


float v = a*b*c;
return v;
}

Computer Programming II Notes Prepared By: - Dhananjay Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Call by Reference:

- We have seen call by reference with traditional C approach in Unit IV Part-I notes,
but In C++ we can also use reference variable.
- Provision of the reference variables in C++ permits us to pass parameters to the
functions by reference.
- When we pass arguments by reference, the formal arguments in the called function
become aliases to the actual arguments in the calling function.
- This means that when the function is working with its own arguments, it is actually
working on the original data.
- Consider Following Function:
void swap(int &a, int &b){ // a and b are reference variable…
a=a+b;
b=a-b;
a=a-b;
}
- Now, if m and n are two integer variables, then the function call
swap(m,n);
- Will exchange the values of m and n using their reference variables a & b.
- Complete Program is as follows:-

#include<iostream.h>
void swap(int &a, int &b){
a=a+b;
b=a-b;
a=a-b;
}
main( ){
int m,n;
cout<< “ Enter m & n\n”;
cin>>m>>n;
swap(m,n);
cout<< “After swapping m=”<<m<< “n=”<<n;
}

Computer Programming II Notes Prepared By: - Dhananjay Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Return by Reference:
- A function can also return a reference.
- Consider following function:

int& max (int &x int &y){


if(x>y) return y;
else return x;
}
main( ){
int a,b;
cout< “Enter two numbers\n”;
cin>>a>>b;
max(a,b)=1;
if(a==1)
cout<< “Largest Number is ”<<b;
else
cout<< “Largest Number is” <<a;
}
- Since return type of max( ) is int&, the function return reference to y or x.
- Then the function call max(a,b) will yield a reference to either a or b depending on
their values.
- In return by reference function call can appear on the left hand side of an assignment
statement.
- That is the statement
max( a,b )=1;
it assigns1 to a if it is smaller otherwise 1 to b.

Computer Programming II Notes Prepared By: - Dhananjay Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

InLine Function:

- One of the objective of using functions in a program is to save some memory space,
which becomes a appreciable when a function is likely to be called many times.
- However, every time a function is called, It takes lots of extra time in executing series
of instructions for task such as
o Jumping to function,
o Saving registers,
o Pushing arguments in to the stack,
o Returning to the calling function.
- When function is small, a substantial percentage of execution time may be spent in
such overheads.
- C++ has solution to this problem. To eliminate the cost of calls to small functions, C+
+ proposes a new feature called inline function.
- An inline function is a function that is expanded in line when it is invoked.
- That is, the compiler replaces the function call with the corresponding function code.
- Syntax of inline function is:
inline Function_Header{
//function body…
}
- Example:-
inline int add( ) {
int a,b,sum;
cout<<”Enter two integers”<<endl;
cin>>a>>b;
sum=a+b;
return sum;
}
main( ){
cout<<”\nThe sum of two numbers is “<<add( )<<endl;
cout<<”\nThe sum of two numbers is “<<add( )<<endl;
cout<<”\nThe sum of two numbers is “<<add( )<<endl;
}
- Compiler may ignore inline function if function definition is too long ot too
complicated & compile the function as a normal function.
- Inline function makes a program run faster but takes extra memory.
- Some Situations where inline function may not work are:
o For function returning values, if a loop, a switch or goto exists.
o For a functions not returning values, if a return statement exits.
o If function contains static variables.
o If inline functions are recursive.

Computer Programming II Notes Prepared By: - Dhananjay Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Default Argument

- C++ allows to call a function without specifying all its arguments.


- In such cases, the function assigns a default value to the parameter which does not
have matching argument in the function call.
- Default values are specified when the function is declared.The compiler looks at
prototype for possible default values.
- A default argument is checked for type at the time of declaration and evaluated at the
time of call.
- Only trailing arguments can have default values & therefore we must add defaults
from right to left.
- We cannot provide a default value to a particular argument in the middle of an
argument list.
- Some examples of function declaration with default values are:
o int add( int=2, int=5, int=10); legal
o int add(int=2, int)illegal
o int add(int=2,int,int=8)illegal
o int add(int,int =2)legal
o int add(int i=2, int j=4) legal.
- Default arguments are useful in situations where some arguments always have same
value.
- A function can be written with more parameters than are required for most common
application.
- Using default arguments , a programmer can use only those arguments that are
meaningful to a particular situations.
- Advantages of default arguments:
o We can use default arguments to add new parameters to the existing functions.
o Default arguments can be used to combine similar functions into one.
- Example:-
#include<iostream.h>
void display(int n, char ch){
for(int i=1;i<=n;i++)
cout<<setw(2)<<ch;
cout<<endl;
}
int main( ){
void display(int = 10, char = ‘D’)
display( );
display(15 );
display(25, ‘M’) }

Output:-
DDDDDDDDDD
DDDDDDDDDDDDDDD

Computer Programming II Notes Prepared By: - Dhananjay Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

MMMMMMMMMMMMMMMMMMMMMMMMM
Constant Arguments:

- const modifier/qualifier is used.


- In C++, an argument to function can be declared as const as shown below,
o int strlen(const char *p);
o int length(const string &s);
- the qualifier const tells compiler that the function should not modify the argument .
- compiler generates error when this condition is violated.
- This type of declaration are significant only when we pass arguments by reference or
pointers.
- Example:-
int len(const char *s){
int i=0;
for( ;s[i]; i++);
return i;
}
main( ){
char *s= “MugDha”;
cout<<len(s);
}

Note:-

Kindly refer your note book for function overloading, friend function & virtual function. We
will discuss these concepts again in details with classes & objects…

Computer Programming II Notes Prepared By: - Dhananjay Joshi

You might also like