Go Back   Gaming Gutter > Non-Gaming > Programming > Tutorials


Tutorials - Looking for programming tutorials to increase your knowledge? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» GG Stuff

Follow us on Twitter!

Get the GG toolbar today (for firefox only)
» Recent Threads
Go to first new post IMMA CRY
Yesterday 09:07 PM
by tealeaf
Last post by Unregenerate Passion
Today 01:20 AM
6 Replies, 49 Views
Go to first new post If you had 24 hours to...
08-11-2009 05:18 AM
by Scythe
Last post by Dr. Ogrish
Today 12:58 AM
45 Replies, 654 Views
Go to first new post Ugly face hot body.
05-05-2009 04:24 PM
by `Kari
Last post by Dr. Ogrish
Today 12:58 AM
69 Replies, 1,801 Views
Go to first new post Milestone Post Count...
01-16-2007 08:54 AM
by nomhak
Last post by Dr. Ogrish
Today 12:56 AM
545 Replies, 8,029 Views
Go to first new post IT'S THE SAME FACE IN...
12-27-2009 11:43 AM
by Shelleh
Last post by Dr. Ogrish
Today 12:55 AM
25 Replies, 481 Views
Reply
 
LinkBack Thread Tools Display Modes

 C++ Pragramming: Chapter 6
Old 05-18-2009, 10:52 AM   #1 (permalink)
Full Member

Male Arachnid is offline
 
Arachnid's Avatar
 
Join Date: Sep 2008
Posts: 738
GPoints: 4,214
iTrader: 0 / 0%
Arachnid Is Popular
Rep Power: 6
C++ Pragramming: Chapter 6

Chapter 6

User Defined Functions

In this chapter I will teach you about functions. Functions are a way to reuse code without having to type it over and over again which is good right? Every console application you make will have at least one function and it is call main. Main is the entry point too, for all console programs. Functions take a while to get used to so lets get started now. First things first is the function skeleton which looks like this:
Code:
return_type function_name(formal_parameters)
{
    statements;
    return _something;
}
This seems pretty simple. But that is all about to change. Formal parameters are really just variable declarations. You pass information to be copied into these newly created variables in the function call by using actual parameters. A formal parameter can consist of any variable type(s) and as many variables as you want. The actual parameters are usually the same amount (don't ask, i think its called overloading a function) and exist in the function call. Now the return statement needs some explaining too, because it is one of the main reasons you have functions (except for void functions which I will discuss later). The return statement is used to give the result of function back to the main function. The return statement causes the current function to terminate and gives control back to the function Main. A good example of a user defined function is the function that finds the absolute value of a number. This example is as follows:
Code:
int absol_val(int num_1)
{
    if(num_1 >= 0)
    {
        return num_1;
    }
    else
    {
        num_1 *= (-1);
        return num_1;
    }
}
Now lets break this down piece by piece. It starts out by declaring that the function absol_val will return an int, no exceptions. It then checks to see whether or not num_1 is below zero or not. If it is not it just returns the number that was copied to it back to main (It is important that you remember that is only a copy and nothing else). If is is below zero, it makes it positive, by saying take num_1, multiply it by (-1) and assign the value to num_1 (in a compact way) all in the statement num_1 *= (-1), and then returns the positive value to the function Main. So no matter what, absol_val will be returning a positive value. now lets talk about function calls and actual parameters. The syntax and an example all rolled into one for the function call to the previous example would be as follows:
Code:
int number;
int num_abs;
cin >> number;
num_abs = absol_val(number);
Think of absol_val as a mathematical equation in which it says the value returned by copying the variable number into the function absol_val where it takes on the name of num_1, then it calculates and returns is assigned into the variable num_abs. That is the basic process as I understand and see it. Now have you ever forgotten to declare a variable but still tried using it. The compiler gets mad and says undeclared identifier! Well functions are like complicated variables. When the compiler sees that you are trying to use the function absol_val but there isn't an absol_val function until later (compilers are like people, in that they don't skip around reading a book, but instead read from left to right, top to bottom) so to the compiler, the function absol_val does not even exist! well this is a problem because I hate putting big ugly functions near the top but instead like to keep things nice and neat. The remedy? Use a function prototype. Its like a little stubbier version of the actual function. The syntax for the absol_val function prototype is:
Code:
int absol_val(int);
It's that simple! You don't even have to say what the variable name will be. So lets put it all together int one C++ program and check it out before we move on to the next part of functions.
Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int absol_val(int);

int main()
{
    int number;
    int num_abs;

    cout << "Enter in the value whose absolute value will be calculated: ";
    cin >> number;

    num_abs = absol_val(number);

    cout << "The absolute value of " << number << " is " << num_abs << endl;

    cin.get();
    return 0;
}

int absol_val(int num_1)
{
    if(num_1 >= 0)
    {
        return num_1;
    }
    else
    {
        num_1 *= (-1);
        return num_1;
    }
}
Now then, that's as simple as I could get it and it still be useful. It declares variables, asks for input, sends a copy of said input to absol_val, checks for its position in relation to zero, returns a positive value, then outputs the value. Pretty simple like I said. Now lets move on to void functions. Void functions main purposes are for output usually. Void functions do not have a return type or a return statement. They are devoid of a value returning ability of any kind. BUT...you can still pass values to the function. The way this works without a return is that it gets to that bottom bracket or final semicolon (not sure which) and then goes to where it left off in the function that made a call to it. It pretty much leaves a bookmark of sorts and then picks right back up where it left off. The syntax for a void function is:
Code:
void function_name(formal_parameters)
{
    stuff;
}
No return type needed. The return type in the function header is just replaced by the keyword void. MOVING ON! What if you don't want to pass a copy to a function but instead want to have a way to have multiple values returned have a value changed by the function instead ooooorrrr you don't want to declare it as a global variable (which is just a variable declared outside of a function and can be seen by anyone and used by any function)? You can do this by passing a reference parameter. A reference parameter is just basically a hexadecimal number that is assigned to a memory location in RAM and it says what ever is in this spot, this is the value I want. Instead of passing a copy, it passes the memories address in RAM and then changes the value by using that address. It's actually part of a later topic called pointers in which the hex number is stored as a variable and that hex number "points" to a variable location. But I digress. The syntax for using a reference parameter is as follows:
Code:
return_type_or_void function_name(var_type &var_name)
{
    stuff;
}
And the function call:
Code:
function_name(&var_name);
The use of the ampersand changes the variable from a copy to the same variable. Of course you can still use different names in the different functions for the variables to keep from confusion but it is all good. Well that is all for this chapter. Just try to make a few programs using this knowledge and if you have any questions ask Checker or |G3| lol jk just ask me and I will do my best to explain it. Until the next chapter guiz, cya around in spamzville or sumthin!

~Arachnid
__________________
Fruitflies.
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Powered by vBadvanced CMPS v3.1.0

All times are GMT -7. The time now is 03:16 AM.


vBulletin skin developed by: eXtremepixels
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.10373092 seconds (100.00% PHP - 0% MySQL) with 21 queries