answersLogoWhite

0

Without functions, a non-trivial program would contain repeated code segments and would be extremely difficult to maintain. Even if you don't write any functions of your own, you still have to call the built-in functions to actually do anything worthwhile.

User-defined functions allow you to structure your source code more efficiently. If you find yourself writing the same code segment over and over, then it makes sense to turn it into a function. Apart from anything else, it reduces the chance of making a mistake or a typo. And with only one copy of the code it becomes that much easier to adjust the function to improve its efficiency or add additional functionality. Moreover, you can break large and complex problems down into smaller functions, each of which can be broken down further if required. This is known as re-factoring. The end result is your code becomes much easier to read and is therefore much easier to maintain.

Every function you write should do a small amount of work, and should do it extremely well and as efficiently as possible. A one-line comment is usually more than sufficient to describe the purpose of a function. But if your function is large and complex, and heavily commented, re-factoring should be considered -- especially if you find it difficult to follow yourself.

While there is a performance penalty in making a function call, your compiler should be intelligent enough to work out which functions can be inline expanded (just as if you'd duplicated the code yourself). In general, only trivial and heavily repeated functions will be inline expanded, and only if the compiler believes there to be an advantage in doing so. While you can tell it which functions to inline expand, the compiler is usually free to ignore the request if it sees no advantage in doing so. Additional optimisers will check the resulting machine code more closely and make finer adjustments as they see fit. The end result should be reasonably efficient code, the only real compromise being overall code size vs. overall code speed (large code is not necessarily fast code).

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Can there be friend functions in c plus plus?

Yes, there can be friend functions in C++.


What is the importance of functions in a c plus plus program?

Functions are very important in C++, as you can't write the simplest program to print hello without using a function. Overall you can say that function are building blocks of a C++ program. Functions can also be defined by the programmer to reduce program size.


What is the importance of using functions in a c program?

C programs do not function without functions.


Printf and scanf Operators in C and C plus plus?

No, they are functions. Operators are -> or ++or /=


Why functions are not used in c plus plus?

Of course they are used. Both stand-alone and class-member functions are used in C++.


What is a method in c plus plus?

In C++, methods are simply class member functions.


How many main functions are used in c plus plus program?

One.


How do you link a C plus plus program to C functions?

It should work without any special action.


Why does VisualStudio c plus plus not have an option to give functions It is in VS c Sharp?

Turn on the intellisense feature.


What are the in-built function in c plus plus?

C++ built-in functions are those functions that are provided for you as part of the language itself, and includes all of the C standard library functions (all of which were inherited from C) and is expanded upon by the C++ standard template library. C++ implementors may provide additional functions that are platform-specific, however these are not considered built-in functions becuase C++ is a cross-platform language. These are best described as 3rd party functions. The functions you yourself write are known as user-defined functions.


What do you call the combination of variables and functions into a single object in C plus plus?

A singleton.


What are the two types of constant in c plus plus?

Constant data and constant functions.