What is Method? A block of instructions/codes defined as - On procedural language(C) – a Procedure On OOP(C++,Java) – a procedure associated with an object class OOP - Specifically an objects behavior
How to declare a Method? Return type Method Name Arguments M ethod Body Structure: return_type method_name (argument list){ instruction 1; …………. Method Body …………. Instruction n; }
Method in Procedural Languages - C They are called - Function Return type – void, int , double etc Function naming, Arguments, Method Body Receive parameters from caller Perform the operations defined on body Return result or void to the caller
Method in OOP – C++,Java Characteristics – Access specifier , Modifier, Return-type, Method name, Parameter list, Prototyping, Method body Acts like objects behavior Also called Member Functions Receive parameters from caller Perform the operations defined on body Return result or void to the caller
Why use Methods? Reusability, Portability Creating abstraction/ conceptual unit Methods can be called recursively Code readability To simplify
Method Example in C
Method Example in C ++ & Java
Famous Methods - C,C++,Java Main function Constructor Abstract Methods Virtual Method