SlideShare a Scribd company logo
Subtopics -
 Functions
 Function Declaration
 Function Arguments
 Return Statements and values
By Hardeep Singh
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( ).
By Hardeep Singh
Function is divided into three
sections
 Function Declaration
 Function Call
 Function Definition
By Hardeep Singh
Form of Function
Syntax: Function Declaration
return_type function_name(parameter list) ;
Syntax : Function Call
int main()
{
Function_name(parameter list);
}
Syntax: Function Definition
{
_ _ _ _ _ _
_ _ _ _ _ _
}
By Hardeep Singh
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);
By Hardeep Singh
 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)
By Hardeep Singh
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.
By Hardeep Singh
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;
By Hardeep Singh
Example
#include<iostream.h>
#include<conio.h>
int factorial(int n);
int main ()
{
int n1,fact;
cout <<"Enter the number whose factorial has to be calculated"
<< endl;
cin >> n1;
fact=factorial(n1);
cout << "The factorial of " << n1 << " is : " << fact << endl;
getch();
return(0);
By Hardeep Singh
}
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);
}
} By Hardeep Singh
Subtopics-
 Parameters Pass/Call by Value
 Parameters Pass/Call by Reference
 Return by Reference
By Hardeep Singh
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.
By Hardeep Singh
Example
#include<iostream.h>
#include<conio.h>
int add(int n);
int main()
{
int number,result;
number=5;
cout << " The initial value of number : " << number <<
endl;
result=add(number);
By Hardeep Singh
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);
} By Hardeep Singh
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.
By Hardeep Singh
Example-
#include<iostream.h>
#include<conio.h>
void swap(int &a,int &b)
{
int t=a;
a=b;
b=t;
}
int main()
{
int m=1,n=2;
By Hardeep Singh
cout<<"Value of m before swapingt"<<m<<endl;
cout<<"Value of n before swapingt"<<n<<endl;
swap(m,n);
cout<<"Value of m after swapingt"<<m<<endl;
cout<<"Value of n after swapingt"<<n<<endl;
getch();
}
By Hardeep Singh
Return by reference
 A function can also return a reference.
 Example:
#include<iostream.h>
#include<conio.h>
int &max(int &x,int &y)
{
if(x>y)
return x;
else
return y;
By Hardeep Singh
}
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;
}
By Hardeep Singh
Subtopics-
Inline functions
Default arguments
Function overloading
By Hardeep Singh
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
} By Hardeep Singh
Example:
#include <iostream.h>
#include<conio.h>
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;
}
By Hardeep Singh
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.
By Hardeep Singh
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
By Hardeep Singh
cout<<"n Final value = "<<amount<<"nn";
printline('='); //use default value for 2nd argument
return 0;
}
float value(float p, int n, float r)
{
int year =1;
float sum = p;
while(year <= n)
{
sum = sum*(1+r);
year = year+1;
}
By Hardeep Singh
getch();
return(sum);
}
void printline(char ch, int len)
{
for(int i=1;i<=len;i++)
printf("%ch",ch);
printf("n");
}
By Hardeep Singh
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
By Hardeep Singh
Example
#include <iostream.h>
#include<conio.h>
class arithmetic {
public:
void calc(int num1)
{
cout<<"Square of a given number: " <<num1*num1 <<endl;
}
void calc(int num1, int num2 )
{ By Hardeep Singh
cout<<"Product of two whole numbers: "
<<num1*num2 <<endl;
}
};
int main() //begin of main function
{
arithmetic a;
a.calc(4);
a.calc(6,7);
getch();
}
By Hardeep Singh

More Related Content

PPTX
C function
PPTX
Function in c
PPTX
ODP
Function
PPTX
Functionincprogram
PPTX
Function in c program
PPT
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
PPTX
Detailed concept of function in c programming
C function
Function in c
Function
Functionincprogram
Function in c program
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
Detailed concept of function in c programming

Similar to functions in c++ basic concept for students (20)

PPTX
Fundamentals of functions in C program.pptx
PPTX
Silde of the cse fundamentals a deep analysis
PDF
Functions in C++.pdf
PPTX
Function in C program
PPT
Unit iv functions
PPT
16717 functions in C++
 
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
PPTX
unit_2.pptx
DOCX
Chapter 5
PPTX
Classes function overloading
PPTX
Fundamental of programming Fundamental of programming
PPTX
functioninpython-1.pptx
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PPTX
unit_2 (1).pptx
PPT
User Defined Functions in C
PDF
unit3 part2 pcds function notes.pdf
PPT
C++ Functions.ppt
PDF
Function in C
PPT
Material 3 (4).ppt this ppt is about the
Fundamentals of functions in C program.pptx
Silde of the cse fundamentals a deep analysis
Functions in C++.pdf
Function in C program
Unit iv functions
16717 functions in C++
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
unit_2.pptx
Chapter 5
Classes function overloading
Fundamental of programming Fundamental of programming
functioninpython-1.pptx
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
unit_2 (1).pptx
User Defined Functions in C
unit3 part2 pcds function notes.pdf
C++ Functions.ppt
Function in C
Material 3 (4).ppt this ppt is about the
Ad

More from soniasharmafdp (15)

PPTX
hashing in data structure for engineering.pptx
PDF
unit 2- linked list- PPT for btech students.pdf
PDF
graphs in data structure for Btech students.pdf
PPTX
New one with new ppt Microsoft Office PowerPoint Presentation.pptx
PPTX
hashing in data structure for Btech .pptx
PPT
classes data type for Btech students.ppt
PPTX
hashing in data structure for Btech.pptx
PPTX
labwork practice on inhetitance-1.pptx
PPTX
friend_derivedclasspresentation1234.pptx
PPTX
OOPS PROGRAM for the clarity of functions in c++
PDF
concept of functions and its types in c++ language
PDF
Data Bill.pdf
PDF
1.3- infix-ti-postfix.pdf
PDF
Linked stack-and-linked-queue
PDF
Datastructureitstypes
hashing in data structure for engineering.pptx
unit 2- linked list- PPT for btech students.pdf
graphs in data structure for Btech students.pdf
New one with new ppt Microsoft Office PowerPoint Presentation.pptx
hashing in data structure for Btech .pptx
classes data type for Btech students.ppt
hashing in data structure for Btech.pptx
labwork practice on inhetitance-1.pptx
friend_derivedclasspresentation1234.pptx
OOPS PROGRAM for the clarity of functions in c++
concept of functions and its types in c++ language
Data Bill.pdf
1.3- infix-ti-postfix.pdf
Linked stack-and-linked-queue
Datastructureitstypes
Ad

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
master seminar digital applications in india
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Classroom Observation Tools for Teachers
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pharma ospi slides which help in ospi learning
PPH.pptx obstetrics and gynecology in nursing
human mycosis Human fungal infections are called human mycosis..pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
master seminar digital applications in india
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.
STATICS OF THE RIGID BODIES Hibbelers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Module 4: Burden of Disease Tutorial Slides S2 2025
Classroom Observation Tools for Teachers

functions in c++ basic concept for students

  • 1. Subtopics -  Functions  Function Declaration  Function Arguments  Return Statements and values By Hardeep Singh
  • 2. 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( ). By Hardeep Singh
  • 3. Function is divided into three sections  Function Declaration  Function Call  Function Definition By Hardeep Singh
  • 4. Form of Function Syntax: Function Declaration return_type function_name(parameter list) ; Syntax : Function Call int main() { Function_name(parameter list); } Syntax: Function Definition { _ _ _ _ _ _ _ _ _ _ _ _ } By Hardeep Singh
  • 5. 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); By Hardeep Singh
  • 6.  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) By Hardeep Singh
  • 7. 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. By Hardeep Singh
  • 8. 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; By Hardeep Singh
  • 9. Example #include<iostream.h> #include<conio.h> int factorial(int n); int main () { int n1,fact; cout <<"Enter the number whose factorial has to be calculated" << endl; cin >> n1; fact=factorial(n1); cout << "The factorial of " << n1 << " is : " << fact << endl; getch(); return(0); By Hardeep Singh
  • 10. } 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); } } By Hardeep Singh
  • 11. Subtopics-  Parameters Pass/Call by Value  Parameters Pass/Call by Reference  Return by Reference By Hardeep Singh
  • 12. 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. By Hardeep Singh
  • 13. Example #include<iostream.h> #include<conio.h> int add(int n); int main() { int number,result; number=5; cout << " The initial value of number : " << number << endl; result=add(number); By Hardeep Singh
  • 14. 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); } By Hardeep Singh
  • 15. 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. By Hardeep Singh
  • 16. Example- #include<iostream.h> #include<conio.h> void swap(int &a,int &b) { int t=a; a=b; b=t; } int main() { int m=1,n=2; By Hardeep Singh
  • 17. cout<<"Value of m before swapingt"<<m<<endl; cout<<"Value of n before swapingt"<<n<<endl; swap(m,n); cout<<"Value of m after swapingt"<<m<<endl; cout<<"Value of n after swapingt"<<n<<endl; getch(); } By Hardeep Singh
  • 18. Return by reference  A function can also return a reference.  Example: #include<iostream.h> #include<conio.h> int &max(int &x,int &y) { if(x>y) return x; else return y; By Hardeep Singh
  • 19. } 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; } By Hardeep Singh
  • 21. 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 } By Hardeep Singh
  • 22. Example: #include <iostream.h> #include<conio.h> int multiply(int); int main( ) { int x; cout<< "n Enter the Input Value: "; cin>>x; By Hardeep Singh
  • 23. cout<<"n The Output is: " << multiply(x); getch(); } inline int multiply(int x1) { return 5*x1; } By Hardeep Singh
  • 24. 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. By Hardeep Singh
  • 25. 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 By Hardeep Singh
  • 26. cout<<"n Final value = "<<amount<<"nn"; printline('='); //use default value for 2nd argument return 0; } float value(float p, int n, float r) { int year =1; float sum = p; while(year <= n) { sum = sum*(1+r); year = year+1; } By Hardeep Singh
  • 27. getch(); return(sum); } void printline(char ch, int len) { for(int i=1;i<=len;i++) printf("%ch",ch); printf("n"); } By Hardeep Singh
  • 28. 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 By Hardeep Singh
  • 29. Example #include <iostream.h> #include<conio.h> class arithmetic { public: void calc(int num1) { cout<<"Square of a given number: " <<num1*num1 <<endl; } void calc(int num1, int num2 ) { By Hardeep Singh
  • 30. cout<<"Product of two whole numbers: " <<num1*num2 <<endl; } }; int main() //begin of main function { arithmetic a; a.calc(4); a.calc(6,7); getch(); } By Hardeep Singh