Hello! This is about creating functions on c++. Actually, creating a function is pretty simple and easy to learn. First, let´s start defining a function. A function, is basically a group of commands arranged inside by a phrase or word that you choose. There are different kinds of functions. C++ has a list of different functions already made and available to use but you can always create your own too.

 

How to create a function? Very easy!

The structure of a function consists in…

 

A type  name ( conditions) { and coding that you want the function to proceed with

 

       A type would be for example: long, double, or integer.

       The name can be almost anything you want, except for the ones already used by c++ to perform another task.

       The values are the things you are declaring.

       and then in between {} you have to type whatever you want the function to do.

 

Here is an example of a function:

 

A function is usually written before and outside the main( ).  Functions are used to isolate different processes in a program. Here´s an example of a simple program; one that works with a function and one that doesn’t and they both do the same.

 

There are different types of functions, for example void doesn’t return any value. For functions that do return values there are different types like double, char, bool, string… When using functions that return values, make sure you make the compiler know to what value return like return a; or return x;

 

To call the function into the main() you can use a cout and type your function.

 

Here´s an example of a non-void function.

                     

  TIPS:

– Remeber to maintain the same type in the same variables on your function.

– Be careful with the braces and were to put them. Always close the function before typing the main( ).

– Be careful with what values you want your function to return… Example: return a; or return 0;

– Be aware that the function name you create can be used on c++ for another thing like main ( ).

– Remeber that if the program compiles, it does not mean it works so make sure you test your program in the compiler.

WATCH MY VIDEO https://youtu.be/NOLXBXg-yik

CC BY 4.0 #Mastery12 – Creating C++ functions by Ricardo Newton is licensed under a Creative Commons Attribution 4.0 International License.