Coding & programming
Installing CodeBlocs compiler
C language
C++ language
© The scientific sentence. 1998-2016
| Â |
|
Programming with C/C++ Language
Introduction to the C++ Language
Functions
1. Functions
We have already used and defined a function which is
the main function. cin.get() is an other example of a function.
In general, functions are blocks of code that perform a number of pre-defined
commands to accomplish something productive.
Functions that a programmer writes will generally require
a prototype. Just like a blueprint, the prototype tells the compiler what the
function will return, what the function will be called, as well as what
arguments the function can be passed.
A function can return a value or not. When it returns
a value, it is used in the same manner as a variable. So a variable
can be set equal to a function that returns a value.
2. Example:
#include // Include rand()
using namespace std; // Make rand() visible
int a = rand(); // rand is a standard function that all compilers have
The integer a is set to the value returned when the function rand()
is called.
The general format for a prototype of a function is :
return-type function_name (arg_type arg1, ..., arg_type argN);
arg_type means the type for each argument, for instance, an int,
a float, or a char. It's exactly the same thing as what you would put
if you were declaring a variable.
There can be more than one argument passed to a function or none at all.
In this case, the parentheses are empty, and it does not have to return a value.
Functions that do not return values have a return type of void.
Here is a function prototype:
int adding ( int x, int y );
This prototype specifies that the function adding will accept two arguments,
both integers, and that it will return an integer. Do not forget the trailing
semi-colon.
When we define a function, we begin with the prototype, without the semi-colon.
Then there should always be a block with the code that the function is to execute,
just as for the main function.
Let's look at an example program:
#include
using namespace std;
// Defining a function
int adding ( int x, int y )
{
return x + y;
}
// Prototype
int adding ( int x, int y );
// Main function
int main()
{
int x;
int y;
cout<<"Enter two numbers to be added: ";
cin>> x >> y;
cin.ignore();
cout<<"The sum of the two entered numbers is "<< adding ( x, y ) << "\n";
cin.get();
}
Using CodeBlocs:
This program begins with the only necessary include file
and a directive to make the std namespace visible.
Everything in the standard headers is inside of the std namespace
and not visible to our programs unless we make them so.
Next is the prototype of the function. Notice that it has the final semi-colon.
When entering numbers, be sure to separate them by a space
so that cin can tell them apart and put them in the right variables.
The main function returns an integer.
The adding function can defined below main. Due to its prototype being
above main, the compiler recognizes it as being defined, and so the compiler
will not give an error about adding function being undefined.
As long as the prototype is present, a function can be used even if
there is no definition. However, the code cannot be run without a definition
even though it will compile.
The prototype and definition can be combined into one also.
If adding function were defined before it is used, we could do away with
the prototype because the definition can act as a prototype as well.
The keyword return is used to force the function to return a value.
Note that it is possible to have a function that returns no value.
If a function returns void, the return statement is valid, but only if it
does not have an expression.
3. Why do we need a function ?
Functions have many uses.
For example, a programmer may have a block of code that he has repeated
several times throughout the program. A function to execute that code would
save a great deal of space, and it would also make the program more readable.
Also, having only one copy of the code makes it easier to make changes
rather making several changes scattered all throughout a potentially
large program .
|
  |