What is Sub Program ?

Sub-program : A sub-program is a named block of code which performs an action whenever it is called with it's name, these sub-program are referred with different name in different programming approaches like function under C/C++ languages, methods in Java and .Net languages and store procedures in data bases.

Method : once after performing an action it may or may not return a value.

How to Define Method ?
  • If we want to define any methods in our program in our program we need to define them under a class as per rule of encapsulation.
  • The methods that we define under a class if require execution need to explicitly called.
  • To call a method explicitly we need create the Instance of the class provide the methods are non-static or else we call the method directly with class name provided the method is static.



Syntax to define a method

[< Modifiers>] void / type<Name> ([< parameter definition>])

  • Modifies are some special keywords which can be used optionally  on a method like public, private, internal, protected, static, virtual, abstract, override, sealed, partial etc
  • Void/type: To specify the method is non value returning or value returning, use void to specify the method is non-value returning where if it returns a value we need to specify the type of value in returns ( which can be any predefined type like int, float, string, object, etc or a user defined type also.)
Note: A non-value returning method just performs an action where a value returning method            performs an action and return result of that action, which can be captured to perform a further action.
  • Name: Refers to the name of the method for identification or calling.
  • Parameters definition:It refers to parameter we want to pass to that method. To make the method dynamic and we pass parameters to a method as following:
                                                  [ref/out] < Type>< name> [=Value]

No comments:

Post a Comment