Page 225 - Cs_withBlue_J_C11_Flipbook
P. 225

{
                            System.out.println(i);
                        }
                    }
                            }
                            void main()
                            {
                                  factor_num();
                            }
                        }
                 b. The function takes the values as parameters but does not return any value.
                   For example,
                    import java.util.*;
                    class circle
                    {
                        void area (double r)
                        {
                          double a;
                            a=3.142*r*r;
                            System.out.println("Area: " +a);
                        }

                       void circumference (double r)
                       {
                            double c;
                            c=2*3.142*r;
                            System.out.println("Circumference: "+c);
                       }

                            void main ()
                       {
                            area(6);              // function calling with parameters
                            circumference(18.6);
                       }
                    }
                 In the above program, the methods area() and circumference() are called along with parameter which calculates the
                 respective parametric values.

                 c.  The function takes the values as parameters and returns a value as well.
                   For example,
                    class average_number
                    {
                        double average(int n)
                        {
                            int i, s=0, c=0;
                            double avg;
                            for(i=1;i<=n;i++)
                            {

                                if(i%2==1)
                                {
                                    continue;
                                }
                                s=s+i;
                                c++;

                                                                                                                       223
                                                                                              Methods and Constructors  223
   220   221   222   223   224   225   226   227   228   229   230