C++ Functions

A function (in programming) is a simple command used to do multiple or more complicated tasks. If you have ever programmed anything in your life, you used a function. Examples of functions are: cin, cout, printf, and getchar. Those are all functions. Now your probably thinking, big deal functions are helpful but why would I need to learn about them? Well functions are great for handling errors and organizing code.

toc_collapse=0;
Contents 

There are four steps to making your own function.
1) The first step is type. You function needs a type, whether it be int, void, or BOOL, it needs one.
2) The second step is naming your function. It needs a name! Imagine calling a dog without a name, it won't go by you. It's the same with functions, in order to call them, you have to name them. Note: Stay away from naming functions something that sounds like it could be declared in header files.
Optional 3)Variables. You can define any function specific variables here. Although this step isn't really needed it can be helpful in organization.
4) Code! Your function has to do something so it needs some code!

Declaring Functions

Declaring common functions: A defined function would be one like LockWindowUpdate. It exists out there as a common function, in a header file that you include (#include). An example is in order to use the function printf in your code, you just include the iostream header file.
#include <iostream>

Declared custom functions: A custom function is what you created. Now, to declare a custom function just look at the very first line of the function you created. Here's a function I'll use in example:

void printMessage(char msgprint){ //this line
printf(msgprint);
}

To declare that, just take the first line (Minus the "{"), and place it up at the top of your file, by the #includes. The declaration for the function posted above looks like this:
void printMessage(char msgprint);
NOTE: You do not have copy the first line if the function code is ABOVE int main.

Return Values/Function Types

Now, remember when you picked the type for you function to be? Well the type depends one what the return value is.
int - Return value is an integer. Example: return 9;
BOOL - Return value is true or false. Example: return false;

Function variables (parameters)

Functions can also have variables. They're special variables, and are called parameters. These are used if you plan to use the function more than once but have a slightly different job. Or they could completely change what the function does, depending on how you use it.
To make a function parameter, declare it in () after the function name. Example:

void printmsg(char* message){ //char* message is the variable for the function
printf("%c", message); //prints the value that you set message to
}

Now when calling the function just do this:
printmsg("hi");//puts the word 'hi' in the console
And now you can use that function to print whatever you want in the console.

Inlining Functions

Some functions, don't really need a function. These kinds of functions are usually only about 2 - 6 lines long and with minimal work. Since functions use up more memory than just using the lines in our code, we can inline them. Now what inlining does is the compiler finds every place you used that function and replaces it with the code inside that function. But it only does this inside the .exe. To inline a function just keep the decloration the same as before, and then go down to the function and add inline in front of the data type. Example:

int main(){
inlinefunction();//calls the function inlinefunction
getchar();//pauses the app
}

inline int inlinefunction(){
printf("inlined function");//prints the message 'inlined function' to console
}

The compiler would turn that code into this:
int main(){
printf("inlined function");//prints the message 'inlined function' to the console
getchar();//pauses the app
}

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.