Search Menu

Functions in C++



Function Overloading and Function Templates

Problem : Why use functions?

Functions not only break up your code into a more organized program, but they can also save time if you need to reuse code often. A function can be called as many times as needed throughout a program; not having to retype the body of the function every time you want to use it saves time and reduces errors.

Problem : What kind of return types can a function have?

A function can return any primitive type, pointers, and objects.

Problem : How can you exit a void function?

A void function will end automatically when it reaches its last line. The return; command can be used to make a void function exit before its last line.

Problem : What is the scope of a static variable in a function?

A static variable is only visible inside of the function in which it is defined, although the function will "remember" the value of the static variable for the next time it is called.

Problem : When should you make a function inline?

A function should be made inline when it is only a few of lines long, and the price of the improved speed of your program outweighs the cost of extra memory. (If you don't understand what that means then just don't use inline and the compiler will make the decision for you).