Is it mandatory to define function outside the class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Is it mandatory to define function outside the class?

23rd Feb 2016, 8:38 AM
NavatejaReddy
NavatejaReddy  - avatar
5 Answers
+ 1
No. It gets much more complicated. Imagine you need a class for one specific function. You create the class in the function, and then use the class in the function. So you can have 4 objects of this class, but it isn't accessible anywhere else... well... because it exists only in the Function.
24th Feb 2016, 1:00 AM
Cooper Scott
Cooper Scott - avatar
+ 1
no
19th Jun 2016, 5:47 PM
Ibrahim Mekraldi
Ibrahim Mekraldi - avatar
0
generally , if we define a function inside a class its request to compiler to act as inline function ... inline function take less time compiling ... so functions which are smaller sized can be defined inside a class ... otherwise ,function which contain a loop, switch case, etc ... should be declare inside class but defined outside the class ..
14th Jun 2016, 11:35 AM
Inder Sandhu
Inder Sandhu - avatar
0
no it is not necessary to define a function outside a class.
10th Jul 2016, 8:35 AM
Rajshree Jain
0
No it is a bad idea to define a function outside a class as it creates complexity.
30th Aug 2016, 12:46 PM
Sachin Sahu
Sachin Sahu - avatar