Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

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