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

Functions in C++ (PF Theory)

This describes the Basic overview on slides for Functions in C++.

Uploaded by

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

Functions in C++ (PF Theory)

This describes the Basic overview on slides for Functions in C++.

Uploaded by

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

Programming Fundamentals

PRESENTATION TOPIC;

“FUNCTIONS in C++”
Group members:
1. Muhammad Shahwaiz- 075
2. Hussnain Ali- 062
3. Ubaidullah-064
Subtopics -
⚫ Functions
⚫ Function Declaration
⚫ Function Arguments
⚫ Return Statements and
values
FUNCTIONS
⚫ Functions are building blocks
of the programs.
⚫ They make the programs more
modular and easy to read and
manage.
⚫ All C++ programs must contain the
function main( ).
⚫ The execution of the program
starts from the function main( ).
Function is divided into three
sections
⚫ Function
Declaration
⚫ Function Call
⚫ Function
Definition
Form of Function
Syntax: Function
Declaration function_name(parameter
return_typ list) ;
e
Syntax : Function Call
int Function_name(parameter
main()
{ list);
}
Syntax: Function Definition
{
______
__ __
__
}
Function Declaration
⚫ A function declaration is made by declaring
the return type of the function, name of the
function and the data types of the parameters
of the function.
⚫ Always terminated by semicolon.
⚫ The general form of function
declaration :- return_type
function_name(parameter list);
⚫ The return_type specifies the type
of the data the function returns.
⚫ The parameter list could be
empty .
⚫ The parameter list should contain
both data type and name of the
variable.
⚫ For example,
int factorial(int n, float j)
Function Arguments

⚫ Arguments contain the actual value which


is to be passed to the function when it is
called.
⚫ The sequence of the arguments in the call
of the function should be same as the
sequence of the parameters in the
parameter list of the declaration of the
function.
⚫ When a function call is made arguments
replace the parameters of the function.
The Return Statement and Return values

⚫A return statement is used to exit


from the function where it is.
⚫ It returns a value to the calling
code.
⚫ The general form of the return
statement is:-
return expression;
Example
#include<iostrea
m>
#include<conio>
int factorial(int n);
int main ()
{
int n1,fact;
cout <<"Enter
the number
whose factorial
has to be
calculated"
<< endl;
cin >> n1;
fact=factor
}
int factorial(int
n)
{ int i=0,fact=1;
if(n<=1)
{
return(1);
}
else
{
for(i=1;i<=n;
i++)
{
fact=fact*i;
}
return(fact)
;
} }
Subtopics-
⚫ Parameters Pass/Call by
Value

⚫ Parameters Pass/Call by
Reference

⚫ Return by Reference
Pass/Call by value

⚫ Copies of the arguments are


created .
⚫ The parameters are mapped to the
copies of the arguments created.
⚫ The changes made to the
parameter do not affect the
arguments.
Example
#include<iostrea
m>
#include<conio>
int add(int n);

int main()
{
int
number,result;
number=5;
cout << " The initial value of number : " <<
number << endl;
cout << " The final value of number : " <<
number << endl; cout << " The result is :
" << result << endl;
getch();
} return(0
);
int add(int number)
{
number=number+
100;
} return(number);
Pass/Call by reference
⚫ Pass by reference is the second
way of passing parameters to
the function.
⚫ The address of the argument is
copied into the parameter.
⚫ The changes made to the
parameter affect the arguments.
Example-
#include<iostrea
m>
#include<conio>
void swap(int
&a,int &b)
{
int t=a;
a=
b;
b=t
;
}
int
cout<<"Value of m before swaping\
t"<<m<<endl; cout<<"Value of n before
swaping\t"<<n<<endl;
swap(m,n);
cout<<"Value of m after swaping\
t"<<m<<endl; cout<<"Value of n after
swaping\t"<<n<<endl;
getch();
}
Return by reference

⚫ A function can also return a


reference.
⚫Example:
#include<iostrea
m>
#include<conio>
int &max(int
&x,int &y)
{
if(x>y)
return
x; else
}
int main()
{
int
m=1,n=2;
max(m,n)=
4;
cout<<"Value of
m"<<m<<endl;
cout<<"value of
n"<<n<<endl; getch();
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
{
}
Example:
#include
<iostream>
#include<conio>
int
multiply(int);
int main( )
{
int x;
cout<< "\n Enter the Input
Value: "; cin>>x;
By Hardeep
Singh
cout<<"\n The Output is: " <<
multiply(x);
getch();
}
inline int multiply(int
x1)
{
return 5*x1;
}
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<iostrea
m>
#include<conio>
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
cout<<"\n Final value =
"<<amount<<"\n\n";
printline(' //use default value for 2nd
='); return argument
0;
}oat value(float p, int n,
fl
float r)
{
int year =1;
float sum =
p; while(year
<= n)

{
sum =
sum*(1+r);
getch();
return(sum);
}
void printline(char ch, int
len)
{
for(int
i=1;i<=len;i++)
printf("%ch",ch);
printf("\n");
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
#include
<iostream>
#include<conio>
class arithmetic
{ public:
void calc(int
num1)
{
cout<<"Square of
a given number: "
{
cout<<"Product of two whole
numbers: "
<<num1*num2 <<endl;
}
};
int main() //begin of main
function
{
arithmetic
a;
a.calc(4);
a.calc(6,

You might also like