Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials

Vote on your favorite hash algorithm here!

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-15-2008, 02:14 PM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 7,399
Last Blog:
Tramp Variables
Credits: 1
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Tutorial: C++ Functions

1. Brief overview of functions:

Functions are segments of code. Their syntax is:

type name ( parameter1) { statements }

With the impact of Java and C# however, functions nowadays aren't that popular. The main difference is of course: classes

Q: What is the main difference between classes and functions?

Answer:
In the OOP everything is about objects. The variables are objects, the keywords are objects, the Main method is an object, the value of the variable is an object. A class can as well contain a function, when a function can't contain a class. The disadvantage though on the classes is that they can be too confusing, namely because everything is seen as object to them. This is confusing from the point of view of the programmer but as well from the compiler, resulting in many compile errors. Functions on the other hand are more "safely typed" in that manner. Consider the following function:

Code:
//a function example
#include <iostream>
using namespace std;

void printmessage ()
{
  cout << "This is a very simple void function in C++";
}

int main ()
{
  printmessage ();
  return 0;
}
Explanation of the function above:

The example above is very basic, though it is interesting to notice one thing: A function can be defined before the Main method.

The function above uses the reserved function printmessage[].

Note, that you can create your own functions. This will be explained later below in this tutorial.

2. Function prototypes:

The function prototype in C++ defines the name of the real function used later, but on the other hand omits the body of the function. It could be considered as the interface of the real function. Though the function prototypes are not obligatory, they might be very handful if you build large and sohphisticated programs, because you can easily see a list of all your functions in your program still in the very beginning of your program.

Code:
#include <iostream> 
#include <cstdlib> 
using namespace std; 


void x(int i); // prototype 
 
int main() 
{ 
  int a = 10;   
  x(a);
 
  return 0; 
} 
 
void x(int i) 
{ 
  i = i * i;  
  cout << i;
}
2.1 Function prototypes before the Main method:

Consider the following example, it uses a function prototype with the bool type, even before the essential Main method:

Code:
#include <iostream> 
using namespace std; 
  
bool four(int num) {  
  if(!(num )) 
      return true; 
  return false;  
}  
  
int main()  
{  
  if(four(4)) 
      cout << "the four word represents the number: 4";  
  if(four(3)) 
      cout << "this is not true";  
  
  return 0;  
}
3. Function parameters:

Understanding the different types of parameters:

Formal Parameters:
The formal parameters are usually specified within the prototype. They are local variables as well.


Actual Parameters:
The values assigned of the function are called: actual parameters. Another name that you can spot on the net describing them is: actual arguments. It i perhaps better to use the second name(arguments). Since you might be confused to understand how a value could be a parameter. .When a function is called the actual parameterrs are assigned to their corresponding formal parameters.


Value Parameters:
As we explained above, the actual parameters are copied at the formal parameter variables each time when a function is called. This parameter passes are called pass by value. Java and C have this parameter passing as their only to any kind of passing through parameters. C++ on the other hand is more advanced and uses a reference type of passing. This is really useful feature of C++ which was later used in C#. It also minimizes the amount ot memory of your application so it can be really lightweight. Perhaps, C++ started the idea of this method of passing and recent news from Microsoft state that the newest version of Windows(Windows 7) will be only 40MB in the core, and we can be almost sure that it will be thanks to the reference types in C#, and they originate from C++ as we mentioned...


Reference Parameters
A reference parameter is declared with the & sign. The compiler than understands that it must pass the address to the real paramater which holds the value. This way obviously you minimize the load of data + the use of new variables. Though this is similar to polymorphism, you can do other things other than what you can do with polymotphism. For example, you can use extremely tight scope in your program. This is not like in the object oriented programming, when you must define a parent class in the polymorphism. However, like we explained this is a different thing.With the reference variables you can as well change the value of the function values.

4. Using functions with vectors:

Vectors contain elements that we intend to use later in our program, these elements are as well stored as arrays. We use vectors with functions, below to illustrate the flixibility of the C++ functions:

Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
using namespace std;

int main()
{
  vector<char *> vectorObject;
  vector<char *>::iterator p;
  int i;

  vectorObject.push_back("A");
  vectorObject.push_back("B");
  vectorObject.push_back("C");
  vectorObject.push_back("D");
  vectorObject.push_back("E");

  cout << "The alphabet, first 5 characters:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;

  cout << "Searching the character: C\n";

  // use a pointer-to-function adaptor
  p = find_if(vectorObject.begin(), vectorObject.end(), not1(bind2nd(ptr_fun(strcmp), "C")));

  if(p != vectorObject.end()) {
    cout << "Found.";
    cout << "From C we have:";
    do {
      cout << *p++ << " ";
    } while (p != vectorObject.end());
  }

  return 0;
}
The output:



5. Virtual functions:

A virtual function is a function whose purpose can be overwritten. Here is a simple example:

Code:
 class car // Base class for C++ virtual function example
     {
       public:
          virtual void drive() // virtual function for C++ virtual function example
          {
               cout<<"Base class Car";
          }
     };

     class CommandButton : public Car
     {
       public:
          void car()
          {
              cout<<"Derived class Command Button - Overridden C++ virtual function\n";
          }
     };

     void main()
     {
         Window  *x, *y;
   
         x = new Car();
         x->drive();
         y = new CommandButton();
         y->drive();
     }


Although, the examples described here are extremely powerful and show you the revolutionary impact of C++(for example its ability to use classes, reference types, user defined functions...), you can do allot more with functions as they are essential part of almost every C++ program. You can play with the examples above and extend more and more "functionality to the functions".
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!

Last edited by Jordan; 04-15-2008 at 02:25 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-18-2008, 11:22 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,535
Last Blog:
wxWidgets is NOT code ...
Credits: 919
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default Re: Tutorial: C++ Functions

Excellent tutorial!
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-22-2008, 02:09 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,553
Last Blog:
Web slideshow in JavaS...
Credits: 1,358
Rep Power: 61
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: Tutorial: C++ Functions

Yes, it's very long - much longer than I would ever dare to write in a single text box, for fear that it would accidentally be deleted or the browser would close (which has happened to me)!
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."
Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Studio 2008: C# Hello World Tutorial Jordan CSharp Tutorials 4 08-22-2008 10:44 AM
Tutorial - ListBox, ComboBox & Command button. travy92 VB Tutorials 9 07-05-2008 11:48 PM
CodeCall Tutorial Contest #4 Jordan Announcements 29 02-25-2008 11:25 AM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM


All times are GMT -5. The time now is 09:03 AM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1083.85
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 903.18
John ........ 902.37
Brandon W ........ 789.89
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 240.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads