why this complication???

hey,i am new in c++..i little confused about why we make things complicated....eg:

#include<iostream>
using name space std;
int main()
{ cout<<"y this strugle"<<endl;
}
we know this program wil out put"y this strugle..."

but why we use another program for the same result..eg

#include<iostream>
using name space std;

void justprint(){
cout<<""y this strugle"<<endl;
}

int main
{

justprint();
}




That is not about making anything complicated. It is about how to define and use functions.

There are MLOC projects out there. (Million Lines Of Code). You don't want all that in main(). You want structure. Logical entities. The code divided into manageable units. Functions make that less complicated.
@keskiverto.....u mean that function are just used for to reduse the compexcity and make the program as manageble???????
In that simple example, there is no particular advantage to the second version.

However if the program needed to do something involving a large number of actions, ways need to be found of simplifying the problem. If we can have main which looks something like this:
1
2
3
4
5
6
int main()
{
    get_user_input();
    perform_calculations();
    print_results(); 
}

then firstly, the overall intention of the program becomes fairly easy to understand and we don't need to be distracted with all the details of what goes on inside each of those functions, and secondly, each of the functions can be worked on separately, without needing to be concerned with all the other parts of the program. Thus it reduces a very complex problem which might seem huge, to smaller steps which are more manageable.
It seems like an overly complicated solution because in the case of your arbitrarily simple example it is. A function is a tool, you are choosing the wrong tool for this job.
@chervil.....ur simply awesome........@computergeek01...wat u mean??
I mean exactly what I wrote, the example you have is too simple to require functions so that is why it seems to be too complicated. Most of the time functions are used so that you don't have to type the same code to perform the same task over and over again, instead you just call the function to perform the task each time it is needed. In your example where one task is being performed one time there is no benefit to using an additional function beyond "Main()".
@computergeek01................thankx u too awesome bro..
Topic archived. No new replies allowed.