Saturday 2 March 2013

Methods


Methods allows the programmer to modularize a program.  All variables declared in method definition are local variables and method parameters are also local variables.  All methods must define inside a class definition.  The general form of a method is as
            Syntax:            type method-name (parameter-list)
                                    {
                                                      // body of the method
                                    }
where,
Ø  type specifies the type of data returned by the method.  If the method does not return any value, its return type must be void
Ø  The method-name is a valid identifier
Ø  parameter-list is a sequence of type and identifier pairs separated by commas.  The variable receives the value of the arguments passed to the method when it is called.  If the method has no parameters, then the parameter list will be empty
Ø  body of the method describes the operations to be performed on the data

Example:         int sum(int a, int b)
                        {
                                          int c;
                                          c=a+b;
                                          return c;
                        }

No comments:

Post a Comment