Left Quote    Maybe this world is another planet's Hell.
- Aldous Huxley    
Right Quote
[login] | [Register]

Beginning Visual C++ 6.0

by: bs0d
Page: 7 of 10
(View All)


Now we'll take a peek at the way functions will operate. We've already touched base with some of the functions that exist, but not how to use them in your programs. To start off, there are 'program-defined functions', 'built-in functions' and 'value-returning functions.' A program- defined function is one you will create on your own! Built-in functions are functions that already exist in the library files (like sine(); and cos();). And a value-returning function is exactly what its called. Once its completed its task, a value is returned to the statement that called the function.

I shouldn't have to go into the advantages of functions, you all should know. Instead of wasting time on that, lets take a look at how the functions will appear, or how you construct them.

a value-returning functions syntax:
returnedDatatype theFunctionName([parameter list]) {
statements here...
return expression;

If thats a bit confusing, just take a look at the sample below:

These might generally be used when called from a statement that is going to show the results of the function, assign the value to a variable or use it in other calcuations.

You see where you declare the parameters for you function? Be aware to the fact that these variables you define are not global; They are to be used only by the function itself, so if you're trying to use them elsewhere, you'll know why you're getting the error.

In your program you will need to declare your function, let c++ know what its all about. This is commonly refered to as the function prototype. So as a habit, before you start int main(); declare your function prototypes of your program defined functions, like this:

Also, when you begin writing your functions, they should be written at the end of the main function like under the comment of the example above.

This is one side of the circle, the other end is calling the function. You can call them in different ways, like within a cout statement, like this:

or set a variable:

If there are no parameters in the function, its considered a void function. You would use these functions to show information, like a form. In the same respect, to call a void-function it will appear on a line by itself. And you need to place VOID before the name of the function ALSO; you dont need a RETURN statement because its not returning a value. Take a look:

Now on to other stipulations like when using more than one parameter in the function, be sure you call the correct number of arguments- take a look:

What im getting at, is just to keep in mind like in the code sample, if I said: answer = aProblem(var1); we would get an error because the way the function was set is to have two variables passed, not just one.

There are two ways in which you can pass information to a built-in function. If its a variable, it can be passed by reference, or by value. This can be a bit confusing but perhaps you'll catch on after seeing samples later on. When the variable is passed by value, only the value is passed. When passed by reference, the address of the variable in the computers memory is passed. If pass by reference, the contents of the variable can change, and not just obtain the value of the variable at the time. At any rate, default is pass by value (for C++).

Passing by Reference

If you choose or your program requires that your function needs access to the variables so that the value of can be changed, you will create the function a bit differently, yet easy to remember. Just include the amperstand (&) character before the variable(s) in your function header, and after each type in the function prototype. Look below:

At first I wasn't going to write all of the code out above, but really to get the full concept its all essential. See how we put the & after the data type in the function prototype, and before the name of the variable in the function header at the bottom. AND do you see now how passing by reference is working? if getSomething(); just took the values of aNumber they would be zero, but instead it obtained the reference to them, and applied the user input number, and they equal whatever was assigned to them.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

1 Total Comments
4 Rating of 5 (2 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· C Tutorial
Your Article Here

"" Copyright © 2002-2019; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank