|
||||||
| C Tutorials All C Tutorials and Code |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
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;
}
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;
}
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;
}
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;
}
![]() 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 Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall Last edited by Jordan; 04-15-2008 at 02:25 PM. |
| Sponsored Links |
|
|
|
|||||
|
Excellent tutorial!
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial - ListBox, ComboBox & Command button. | travy92 | VB Tutorials | 9 | 07-05-2008 11:48 PM |
| Visual Studio 2008: C# Hello World Tutorial | Jordan | CSharp Tutorials | 1 | 02-27-2008 07:48 AM |
| 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 |