SlideShare a Scribd company logo
Function in c++
Function
Function
 Dividing a program into functions.

 ■ a major principle of top-down, structured programming.

 ■ To reduce the size of the program.

 ■ Code re-use.

 ■ Like C++ operators, a C++ function can be overloaded to make it perform different tasks
depending on the arguments passed to it.
Introduction
 Void show(); /* Function declaration */
 void main()
 {
 show();
 /* Function call */
 }
 void show()
 /* Function definition */
 {
 /* Function body */
 }
The main() Function
 The main() returns a value of type int to the operating system by default.
 The functions that have a return value should use the return statement for termination.
 Use void main(), if the function is not returning any value.
Function Prototyping
 The prototype describes the function interface to the compiler by giving details such as:
 The number and type of arguments
 The type of return values.
 ■ It is a template
 ■ When the function is called, the compiler uses the template to ensure that proper arguments are
passed, and the return value is treated correctly.
 Each argument variable must be declared independently inside the parentheses.
 Float avg (int x, int y); // correct
 float avg (int x, y) ;
 // illegal
 ■ In a function declaration, the names of the arguments are dummy variables and therefore they
are optional.
Call by Value
 Function call passes arguments by value.
 The called function creates a new set of variables and copies the values of arguments into them.
 The function does not have access to the actual variables in the calling program and can only work
on the copies of values.
Call 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 theoriginal data.
Advantages of Functions
 Easier to Code
 Easier to Modify
 Easier to Maintain
 Reusability
 Less Programming Time
 Easier to Understand
Importance of Function
 A program may need to repeat the same piece of code at various places.
 It may be required to perform certain task repeatedly.
 • The program may become very large if functions are not used.
 The real reason for using function is to divide program into different parts
Function Overloading
 ■Can enables several function
 ■ Of same name
 ■ Of different sets of parameters (at least as far as their types are concerned)
 ■ Used to create several functions of the same name that perform similar tasks but on different
data types

More Related Content

PPTX
Functions and modular programming.pptx
PPT
Material 3 (4).ppt this ppt is about the
PPTX
Chapter One Function.pptx
DOCX
Functions assignment
PPTX
C++_Functions_Detailed_Presentation.pptx
PPTX
Function Overloading Call by value and call by reference
PDF
Functions in C++.pdf
PPTX
Functions in C++ (OOP)
Functions and modular programming.pptx
Material 3 (4).ppt this ppt is about the
Chapter One Function.pptx
Functions assignment
C++_Functions_Detailed_Presentation.pptx
Function Overloading Call by value and call by reference
Functions in C++.pdf
Functions in C++ (OOP)

Similar to Functions in c++, presentation, short and sweet presentation, and details of functions (20)

PPTX
Chapter 6 - Modular Programming- in C++.pptx
PDF
VIT351 Software Development VI Unit1
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PPTX
Functions in C++
PDF
Chapter 5 - Modular Programming.pdf
PPT
FUNCTIONS IN c++ PPT
PPTX
Function C++
PPTX
PPT
16717 functions in C++
 
PDF
Cpp functions
PPT
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PPTX
Functions1
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PPTX
Classes function overloading
PPTX
Chapter 1 (2) array and structure r.pptx
PPTX
Functions in c language
PPTX
Functions in c language
DOCX
Functions in c++
Chapter 6 - Modular Programming- in C++.pptx
VIT351 Software Development VI Unit1
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
Functions in C++
Chapter 5 - Modular Programming.pdf
FUNCTIONS IN c++ PPT
Function C++
16717 functions in C++
 
Cpp functions
FUNCTIONS, CLASSES AND OBJECTS.pptx
Functions1
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Classes function overloading
Chapter 1 (2) array and structure r.pptx
Functions in c language
Functions in c language
Functions in c++
Ad

Recently uploaded (20)

PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Geodesy 1.pptx...............................................
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
DOCX
573137875-Attendance-Management-System-original
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Sustainable Sites - Green Building Construction
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Geodesy 1.pptx...............................................
Embodied AI: Ushering in the Next Era of Intelligent Systems
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
573137875-Attendance-Management-System-original
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Internet of Things (IOT) - A guide to understanding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Sustainable Sites - Green Building Construction
bas. eng. economics group 4 presentation 1.pptx
OOP with Java - Java Introduction (Basics)
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CH1 Production IntroductoryConcepts.pptx
Construction Project Organization Group 2.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Ad

Functions in c++, presentation, short and sweet presentation, and details of functions

  • 2. Function  Dividing a program into functions.   ■ a major principle of top-down, structured programming.   ■ To reduce the size of the program.   ■ Code re-use.   ■ Like C++ operators, a C++ function can be overloaded to make it perform different tasks depending on the arguments passed to it.
  • 3. Introduction  Void show(); /* Function declaration */  void main()  {  show();  /* Function call */  }  void show()  /* Function definition */  {  /* Function body */  }
  • 4. The main() Function  The main() returns a value of type int to the operating system by default.  The functions that have a return value should use the return statement for termination.  Use void main(), if the function is not returning any value.
  • 5. Function Prototyping  The prototype describes the function interface to the compiler by giving details such as:  The number and type of arguments  The type of return values.  ■ It is a template  ■ When the function is called, the compiler uses the template to ensure that proper arguments are passed, and the return value is treated correctly.
  • 6.  Each argument variable must be declared independently inside the parentheses.  Float avg (int x, int y); // correct  float avg (int x, y) ;  // illegal  ■ In a function declaration, the names of the arguments are dummy variables and therefore they are optional.
  • 7. Call by Value  Function call passes arguments by value.  The called function creates a new set of variables and copies the values of arguments into them.  The function does not have access to the actual variables in the calling program and can only work on the copies of values.
  • 8. Call 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 theoriginal data.
  • 9. Advantages of Functions  Easier to Code  Easier to Modify  Easier to Maintain  Reusability  Less Programming Time  Easier to Understand
  • 10. Importance of Function  A program may need to repeat the same piece of code at various places.  It may be required to perform certain task repeatedly.  • The program may become very large if functions are not used.  The real reason for using function is to divide program into different parts
  • 11. Function Overloading  ■Can enables several function  ■ Of same name  ■ Of different sets of parameters (at least as far as their types are concerned)  ■ Used to create several functions of the same name that perform similar tasks but on different data types