Gw Temp

Menu

Tutorial - 'C++ Part 2 Tutorial: Functions' by lithium

An item about Programming Languages posted on

Blurb

Just when you thought it was safe to go away from your computer...

Body

Standard ANSI/ISO C++ Tutorial: Part 2 'Kazeuri Sucks'


Woopia! Back again with part two. In this part, we will cover Functions! Aren't you excited?
Well then, lets get started.

Functions Amouns-ctions
************************

A function that adds two intergers might return a sum, and thus, would be defined to return an interger value. If you have a function that just prints a message (i.e. cout strings) has nothing to give in return and thereford gets declared to return void. I know, I hate that word too. One time I was playing Might and Magic and I had to go into the void and... oh, sorry, back to the tutorial.

A function consists of two parts: a header and a body. The header contains the return type, the name of the function and all the little parameters that snuck thier way in and stole your granola bar. Parameters allow a fuction to pass values through itself. Thus, if the function were to add two numbers , those would become parameters to the function. Here is a little sample of a usual function header:

Example 2.1
int Sum(int a, int b)

Parameter is a declaration of what type of value will be passed in: the actual value passed in by the calling function is called the argument (that of which I have with the voices in my head frequently). If you are a frequent programmer, or you are just a C++ nerd as myself, you would refer to the terms, parameters and arguements, as synonyms.

The name of the function and its parameters (header without a return value) is called the signature.

The body contains of a opening brace (which if you read my previous edition to this tutorial series, 'space monkey chatter' you would have known, so if you haven't read it. go. now. read. it. before I kill you. NOW.), zero or a number of statements and then a closing brace (go read the spacemonkey chatter tutorial if you have no idea what a brace does. read it. now. me and bart will kill you if you don't. I'll send faust to your house, and you don't want that, believe me). The statement consists of what teh function needs to do. The function may respond byreturning a value using a return statement. Once there is no other statements for the function to perform, the function will cease and the code will continue the line after the last command the last function performed (most likely the last action would be a closing brace, actually, 99% of the time it is the closing brace, but sometimes people use getline.end or cin.endfunc as a closing brace command - but that seems a little more complicated than what you've gotten through thus far in the tutorials I've written). If the function cannot locate a return function in your statement, any value that the function was suppose to produce will be automatically void when you end the function (bitch ain't it?). The value that a function returns MUST, I repeat, MUST be of the type listed in the header of the function or you could get some nasty complier errors such as:

Error compiling near line 6: wrong type declared

I hate that error. I get it alot. Don't mess up and forget to make sure it all works, because one little error can make any C++ program crash.

This next example will demostrate two interger parameters and shows you how it returns a value. Don't worry about the little coding thingies that you don't see yet. I'll explain them in later parts, just know that you have to remember those because that is called Syntax, the cunning swift creator of compliling errors as I like to call the little bastard.

Example 2.2
***********
#include
int Add (int x, int y)
{
cout << "In Add(), received " << x << "
and " << y << "\n";
return (x+y);
}

int main()
{
cout << "I'm in main()!\n";
int a, b, c;
cout << "Enter two numbers: ";
cin >> a
cin >> b;
cout << "\nCalling Add()\n";
c=Add(a,b);
cout << "\nBack in main().\n";
cout << "c was set to " << c;
cout << "\nExiting...\n\n";
return 0;
}

On the line 2, the function Add() is defined. It took the two parameters and returned a value. The actual script that would display starts on line 11 where it uses the cout function to display a message to the screen. Then, the scipt asks the user for two numbers ( in this case, the numbers I choose were 7 and 2. When you type each number, separated by a space, and then press enter, main() processes the two numbers and labels them as arguments (or parameters, which ever one makes you more hot) to the Add() function on line 17, which then carries them to be added and if you put in 7 and 2 as I did, you would get a 9 back.

Example 2.3: EndOutput
*********************
Your end output from, depending on your numbers, woud like something like this:
I'm in main()!
Enter two numbers: 7 2

Calling Add()
In Add(), received 3 and 5

Back in main().
c was set to 9
Exiting...
Press any key to continue...

See, you can learn from me. In teh next part i'll go over variables so all you GW-monkeys can learn more. For now, Pz out.