Function

From Chilipedia
Jump to: navigation, search

A function is a grouping of code that you can invoke or 'call' from one or more points in your program. You can even call a function from itself (motherfucking funception)!

Function Calls

A function call in C++ generally looks something like this obj.MyFunction( param1,param2 ). It consists of the function name (Myfunction in the example above) followed by parentheses () which surround parameters separated by commas (,). To call a member function of an object, you first type the object name, then a period '.', and then the function name (e.g. obj.).

The function parameters are used to control the behavior of the function. A function can have zero or more of these. A function can also return a value, which can be used in other operations. If a function does not return a value, its return type is void.

Declaring and Defining Functions

Declaring a function is pretty straight forward as described above. You have the three parts; the return type, the name, the parameter list. Functions can be declared as global functions or member functions. Global functions are functions that do not belong to a class or struct, which would be called member functions. You can declare a global function in the header file (.h) or in the source file (.cpp).

Examples: A global function that returns nothing and doesn't take any parameters.

void DoSomething();

A global function that returns nothing and takes two parameters.

void PrintAandB(int A, int B);

A global function that returns an int, and takes two int parameters.

int AddThese(int A, int B);

A member function is declared in the same manner, the only difference is they are declared inside the class declaration.

class MyClass
{
    void DoSomething();
    void Print(int A, int B);
    int AddThese(int A, int B);
};

Defining functions is handled differently depending on where you define them and whether they are global or member functions. A functions can be declared and defined in the same place. If a global function is declared and defined in the header file, it must be preceded with the word inline, letting the compiler know that you are defining it there instead of trying to look for it in a source file.

inline void DoSomething()
{
     // Put code here...
}


If you declare the function in the header file and define it in the source file, you don't need the inline declaration.

If you declare and define a member function in the class declaration, you also do not need the inline declaration.

class MyClass
{
    int AddThese(int A, int B)
    {
         return  A + B;
    }
};

What's really goin on here tho

What actually happens when a function is called is: the values to be passed to the function are placed somewhere where the function code can access them (called the stack, or in some calling conventions the parameters are put into CPU registers), then the current execution address is saved and the CPU jumps to the function address and begins executing the function instructions. At the end of the function, the result (if any) is placed in a predetermined location (typically a register), and the CPU jumps back to the address that it had previously saved and continues on its merry way.

Tutorial Videos

Beginner C++ Game Programming Tutorial 1

See Also

http://www.cplusplus.com/doc/tutorial/functions/