0% found this document useful (0 votes)
5 views

Week2 Functions

The document provides an overview of functions in C++, including their advantages, types (built-in and user-defined), and components such as prototypes and definitions. It explains various function categories, including those with and without return values and arguments, as well as concepts like pointers, reference variables, call by value, and call by reference. Additionally, it covers advanced topics like inline functions, default arguments, and function overloading.

Uploaded by

hurraysmart141
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Week2 Functions

The document provides an overview of functions in C++, including their advantages, types (built-in and user-defined), and components such as prototypes and definitions. It explains various function categories, including those with and without return values and arguments, as well as concepts like pointers, reference variables, call by value, and call by reference. Additionally, it covers advanced topics like inline functions, default arguments, and function overloading.

Uploaded by

hurraysmart141
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

CSET334 - Programming using C++

Module 2: FUNCTIONS
FUNCTIONS
Void main() Void main()
{ {
Statement 1; Statement 1;
Statement 2; Statement 2;
Statement 3; Sum1();
. Statement 3;
. Statement 4;
. Sum2();
. Statement 5;
Statement n; Statement 6;
} }
Advantages
• Support for modular programming
• Reduction in program size.
• Code duplication is avoided.
• Code reusability is provided.
• Functions can be called repetitively.
• A set of functions can be used to form libraries.
Types
1.Built in functions :-
are part of compiler package.
Part of standard library made available by compiler.
Can be used in any program by including respective header file.

2. User defined functions:-


Created by user or programmer.
Created as per requirement of
the program.
User defined function
Void main()
{
Statement 1;
Statement 2;
multiply();
Statement3;
;
;
Sum();
return0;
}

multiply()
{
;
}
Parts of a function
main function
{
function prototype declaration
function call
-------;
}

function declaratory/definition
{
------;
return statement
}
Function prototype
A function prototype is a declaration of a function that tells the program about
the type of value returned by the function, name of function, number and type
of arguments.
Syntax: Return_type function_name (parameter list/argument);

int add(int,int);
void add(void);
int add(float,int);

4 parts Variable declaration


i. Return type Data_type variable_name ;
ii. Function name int x=5;
float marks;
iii. Argument list int price;
iv. Terminating semicolon
Function call
A function must be called by its name followed by argument list
enclosed in semicolon.
Syntax: function_name (parameter list/argument);

add(x,y);
add(40,60);
add(void); or add();
Note: data type not to be mentioned.

Suppose int add(int,int); //prototype


Now to this function add(x,y); //function call
or
add(40,60);
Suppose int add(int,float); //prototype

add(x,y); //function call

int float

Now suppose the function return some integer value, you can use a
variable to store that value.
i.e z=add(x,y);
Parts of a function
main function
{
function prototype declaration
function call

}
function declaratory/definition
{
return statement
}
Function definition 2 parts
Syntax: function_type function_name(parameter Function
list) { header
Note: no semicolon in
Local variable declaration; header

Function
Function body statement;
body
Return statement;
}
Example: int add(int,int); //prototype
Z=add(x,y); //function call

int add(int a,int b) //function definition


{
body statement;
Return(a+b);
} j
#include<iostream.h>
#include<iostream.h>
using namespace std;
using namespace std;
int main()
void print()
{
Print();
{
cout<<“2 is even no.”<<endl;
return 0; Error message since
print was not declared in }
} the scope.
int main()
void print()
{
{
Print();
cout<<“2 is even no.”<<endl;
return 0;
}
}
Why prototyping?????
#include<iostream.h>
using namespace std;
void print(); Return_type function_name (parameter
list/argument);
int main()
{
print(); function_name(parameter list/argument);
return 0;
}
void print() function_type function_name(parameter list)
{
cout<<“2 is even no.”<<endl;
}
Parts of a function
main function
{
function prototype declaration Return_type function_name(arguments); eg: int add(int);
function call
function_name(actual arguments); eg: add(a);
-------;
}

function declaratory/definition Return_type function_name(formal arguments) eg: int add(int X);


{
------;
return statement
}
Function categories
i) Function with no return value and no argument.
void add(void);
ii) Function with arguments passed and no return value.
void add(int,int);
iii) Function with no arguments but returns a value.
int add(void);
iv) Function with arguments and returns a
value. int add(int,int);
I. Function with no return value and no argument
void main()
{
void disp(void); //prototype
No value returned from
disp(); //caller function No arguments passed
calle to caller function return 0; from caller to calle
}

void disp() //calle function


{
cout<<“--------”<<endl;
}
//program to print square of a number using functions.
void main()
{
void sqr(void);
sqr();
getch();
return 0;
}

void
sqr()
{
int no;
cout<<“enter a no.”;
cin>>no;
cout<<“square
of”<<no<<“is”<<no
*no;
ii. Function will not return any value but passes argument
#include<iostream.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
add(a,b);
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
a
getch();
return 0; b
}
void
add(int
x,int y)
void add(int x,int y);
{
int c;
c=x+y;
cout<<
iii) Function with arguments and return value
main function
{
int sqr(int); //function prototype
int a,ans;
cout<<“enter a number”;
cin>>a;
ans=sqr(a); //function call
cout<<“square of number is”<<ans;
getch();
return 0;
}

int sqr(int X) //function declaratory/definition


{
return(X*X);
}
iv) Function with no arguments but returns a value
int main()
{
int add(void); Function call
int z; add(x,y);
z=add();
i.e z=add(x,y);
cout<<sum
of 2 nos
is”<<z;
getch();
return 0;
}int add(void);
{
int a,b;
cout<<“enter 2 nos”;
cin>>a>>b;
return(a+b);
}
Pointers
• Special type of variables which hold the address of another variable
i.e no values or datas are stored but it points to another variable
where data is stored.

int a=100;
Pointer stores
1)a name this memory
location, no
direct value is
100  2)value stored.
3)address
a
Declaration: Data_type *variable_name;
100
int a=100; address 1
int *ptr;
Intialisation: ptr=&a; //
“Address of” operator referencin
ptr
g
or address 1
int a=100;
Pointer initialisation
int *ptr=&a; address 2
and declaration
Note: a pointer can be used to point to another pointer also i.e it can
store the address of another pointer.
a
100
int a=100; address 1 cout<<a; //100
int *ptr=&a; cout<<*ptr;//100
ptr cout<< **ptr1; //100
int **ptr1=&ptr;
address 1
address 2
Note: pointer 1 points to address of ptr.
ptr1
address 2
address 3
#include<iosream.h> Chain of pointers
#include<conio.h> p1 p2
a
int main()
100 address 1 address 2
{
int a=100; address 1 address 2 address 3
int *p1;
int * *p2;
p1=&a; //p1 points to address of a
p2=&p1; // p2 points to address of p1
cout<<“address of a”<<&a;
cout<<“address of a”<<p1;
cout<<“value of a”<< *p1;
cout<<“value of a”<< * *p2;
cout<<p2;
cout<< *p2;
getch();
}
Reference variable in C++
When a variable is declared as reference, it becomes an alternative name
for an existing variable. A variable can be declared as reference by
putting ‘&’ in the declaration.
int a=100;
Now we will use a reference variable i.e two names
for same memory location.
a
int a=100;
100 int &ref=a; //initialization and declaration

0012dcD2 Now in program we can use either “a” or an


alternative name “ref”

C=a+b; //same
output C=ref+b;
Program using reference variable
#include<iostream.h>
#include<conio.h>
int main() 100
{ 100
int a=100; 0012dcD2
int &ref=a; 0012dcD2
cout<<“va
lue of a
is”<<a; Both “a” and “ref” are used for
same memory location as
cout<<“va alternative name
lue of ref
is”<<ref;
cout<<address of a is”<<&a; P.P.Krishnaraj
RSET
Call by value
A function can be invoked in two manners
(i) call by value
(ii) call by reference
The call by value method copies the value of actual parameters into formal
parameters i.e the function creates its own copy of arguments and uses
them.

Values of variables a
add(a,b); and b are passed to

Call by value
} X,Y

where the values of void add(int x,int y); Now if you change
variable are passed
to functions { the value of X and Y,
those changes are
--------; not seen in a and b

}
/* program to illustrate the concept of call by value
*/
#include<iostream.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
getch();
return 0;
}
void
add(int
x,int y)
{
int c;
cout<<“addition is”<<c;
c=x+y;
Call by reference
In call by reference method in place of calling a value to the function being
called , a reference to the original variable is passed .i.e the same variable value
can be accessed by any of the two names.
In function call
add(a,b); We write reference
variable for formal
} arguments

No need for return


void add(int &x,int &y); &X,&Y will be the
statement { reference variable for
a and b. if we change
--------; X and Y, Value of a
and b are changed
} accordingly
Program to illustrate call by reference swap(a,b);
cout<<“after swaping”;
#include<iostream.h>
cout<<“A”<<a;
#include<conio.h>
cout<<“B”<<b;
void swap(int &,int &);
getch();
int main()
}
{
int a,b;
void swap(int &X,&Y)
cout<<“enter the values of a and b”;
{
cin>>a>>b;
int temp;
cout<<“before swaping”;
temp=X;
cout<<“A”<<a;
X=Y;
cout<<“B”<<b;
Y=X;
}
Return by reference

⚫ A function can also return a }


reference. int main()
⚫Example: {
#include<iostrea int
m.h> m=1,n=2;
#include<conio.h max(m,n)=
> 4;
int &max(int cout<<"Value of
&x,int &y) m"<<m<<endl;
{ cout<<"value of
if(x>y) n"<<n<<endl; getch();
return return 0;
Subtopics-
⚫ Inlinefunctions
⚫ Default

arguments
⚫ Function

overloading
Inline Functions
⚫ An inline function is a function that expanded
in line when it is invoked.
⚫ That is the compiler replaces the function call with
the corresponding function code .
⚫ Syntax:
inline function-header
{
Function body
}
Example:

#include <iostream.h> getch();


#include<conio.h> }
int multiply(int); inline int multiply(int x1)
{
int main( )
{ return 5*x1;
}
int x;
cout<< "\n Enter the Input Value: ";
cin>>x;
cout<<"\n The Output is: " <<
multiply(x);
Default Arguments
⚫ Default values are specified when the
function is declared.
⚫ Compier looks at the prototype to see how
many arguments function uses.
⚫ Default arguments are useful in situations
where some arguments always have the
same value.
Example
#include<iostream.h>
#include<conio.h> int
main()
{
float amount;
float value(float p,int n,float r=0.15); //prototype
void printline(char ch='*',int len=40); //prototype
printline(); //uses default values for argumennts amount =
value(5000.00,5); //default for 3rd argument
cout<<"\n Final value =
"<<amount<<"\n\n";
printline(' //use default value for 2nd
='); return argument
0; getch();
}oat value(float p, int n,
fl return(sum);
float r) }
{ void printline(char ch, int
int year =1; len)
float sum =
{
p; while(year
<= n) for(int
i=1;i<=len;i++)
{ printf("%ch",ch);
sum = printf("\n");
sum*(1+r);
}
Function Overloading

⚫A function is overloaded when same name is


given to different function.
⚫ The two functions with the same name will
differ at least in one of the following.
a) The number of parameters
b) The data type of parameters
c) The order of appearance
Example cout<<"Product of two whole
#include numbers: "
<iostream.h> <<num1*num2 <<endl;
#include<conio.h }
> class arithmetic
};
{ public:
int main() //begin of main function
void calc(int
{
num1)
{ arithmetic
cout<<"Square of a;
a given number: "
a.calc(4);
<<num1*num1
{
<<endl; a.calc(6,
Thank You

You might also like