Functions in Java

21,545 views

Published on

Pure and impure functions

Published in: Education, Technology
3 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
21,545
On SlideShare
0
From Embeds
0
Number of Embeds
77
Actions
Shares
0
Downloads
203
Comments
3
Likes
3
Embeds 0
No embeds

No notes for slide

Functions in Java

  1. 1. FUNCTIONS/METHODS IN JAVA
  2. 2. FUNCTION• A program module used simultaneously at different instances in the program is called method or function
  3. 3. Defining a function• <Access specifier><Return type><method name>(parameter list)• {• //Body of the function• }• Access specifier – public or private.• Method declared without access specifier is by default treated as public• Type- Specifies the data of the value returned from the method• Function name – Preferably related to the program• Parameter list – Variables which receives the value passed from the arguments during method call
  4. 4. Components of a method• There are two components• Header ( also known as method prototype)• Body• List of parameters is called signature
  5. 5. Header Public int add() Body return(value) The statement which sends back the value from a method is caller programReturn statement is used at the end of the program which is a function terminator
  6. 6. Types of function Function Pure Function / Impure Function /Accessor method Mutator method
  7. 7. Pure function Pure Function / Accessor method Class Function called Function Calculations done Value returnedA function which returns a value to its caller module
  8. 8. Program for pure function class add { void sum(int a, int b) { int c = calc(a,b); System.out.println(“The sum is” +c); } int calc(int a, int b) { Int c=a+b; return(c); } }
  9. 9. Impure function impure Function / Accessor method Class Function called Function Calculations done Value not returnedA function which does not return a value .It will be a void functionChanges the state of the object each time the function is called
  10. 10. Program for pure function class add { void sum(int a, int b) { calc(a,b); } void calc(int a, int b) { int c=a+b; System.out.println(“The sum is” +c); } }
  11. 11. Different ways of defining a function• Receiving value and returning outcome to the caller• Receiving values but not returning outcome to the caller• Neither receiving values nor returning outcome to the caller
  12. 12. Formal parameter• Parameter is a variable used with method signature that receives a value during function call• When the parameter values are received from the caller then it is called Formal parameter
  13. 13. Actual Parameter• If the values are passed to the method during its call from the caller then it is actual parameter
  14. 14. Pass by value• Process of passing a copy of actual arguments to the formal parameters.• Any change made in the formal will not reflect on actual argument
  15. 15. Formal parameters after function operation Actual arguments before function call X Y A B 7 8 5 6 Actual arguments after function call A BCopied formal parameters during function call 5 6 X Y 5 6
  16. 16. PASS BY REFERENCE• Process of passing the reference of actual arguments to the formal parameters and any change in the formal parameters will be reflected in the actual parameters also
  17. 17. Function overloading
  18. 18. • Function with the same name but used for different purposes is called as overloaded functions• It is implemented through polymorphism• Compiler finds the best match of the function arguments and the parameter list during program compilation called static binding
  19. 19. Recursive function• A function which calls by itself is called as recursive function

×