0% found this document useful (0 votes)
84 views13 pages

User Defined Methods or Functions Grade 10 B

The document discusses user defined methods or functions in Java, explaining that a function is a sequence of statements grouped together and given a name that can be called to perform a specific task, and differentiates between pure and impure functions as well as call by value and call by reference passing. It also provides examples of function prototypes and definitions as well as a program to find the factorial of a number and calculate a value using factorials.

Uploaded by

Dwijesh Donthy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
84 views13 pages

User Defined Methods or Functions Grade 10 B

The document discusses user defined methods or functions in Java, explaining that a function is a sequence of statements grouped together and given a name that can be called to perform a specific task, and differentiates between pure and impure functions as well as call by value and call by reference passing. It also provides examples of function prototypes and definitions as well as a program to find the factorial of a number and calculate a value using factorials.

Uploaded by

Dwijesh Donthy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 13

User defined Methods or Functions

Grade 10 B
Define a function

 A function or a method is a sequence of statements


grouped together and given a name. This group of
statements can be called at any point in the program
using its name to perform a specific task.
Two ways of invoking functions are:

1. Pass by value.

2. Pass by reference.
Differentiate between pure and impure functions.

 Pure functions  Impure functions


 Pure functions take objects and/or  Impure functions change the state of
primitive data types as arguments received objects..
but does not modify the objects.  Impure functions have side effects
 Pure functions doesn't have side
effects.
Call by value Call by reference
Actual parameters are copied to formal Formal parameters refer to actual
parameters. Any changes to formal parameters. The changes to formal
parameters are not reflected onto the parameters are reflected onto the
actual parameters. actual parameters.
All reference data types like arrays and
All primitive data types are passed
objects of classes are passed using Call
using Call by value.
by reference.
Formal parameter Actual parameter
Formal parameters appear in Actual parameters appear in
function definition. function call statement.
They represent the values They represent the values passed to
received by the called function. the called function.
void test1(int n)
{
for(int x=1; x<=n; x++)

if(x%n == 0)
System. out.println(x);

}
if 12 is passed to n.
void test3(char c)
{
System.out.println( (int) c);
}
if 'm' is passed to c.
Write a program to input a number. Use a function int
Armstrong(int n) to accept the number. The function
returns 1, if the number is Armstrong, otherwise zero(0).
Sample Input: 153
Sample Output: 153 ⇒ 13 + 53 + 33 = 153
It is an Armstrong Number.
Prototype:
function header is called as
 Create a prototype of function sum which returns a float value and takes two
integer augments .
 Function access specifier return type function name
parameter list
 public float sum(int a , int b)
 {
 }
Create a function prototype of Student
which return string value and takes one
float , one double and one int arguments.
 public String Student (float a, double b, int c)
 {
 }
Write a function fact(int n) to find the factorial of a number n. Include a main
class to find the value of S where:
S = n! / (m!(n - m)!)
where, n! = 1 x 2 x 3 x .......... x n

You might also like