please show example n explanation...thanks...
User define function,c++ please...?
a function basically has two parts to it
%26lt;return type%26gt; function_name(%26lt;parameter types)
{
//code goes here
// code goes here
// this function must return a value if has a datatyp other than void
}
take for example the main function:
#include %26lt;iostream%26gt;
void main()
{
using namespace std;
}
this main function doesn't have a return datatype nor does it have datatype in parameters
now let's create a function that calculates the exponent of a number:
int exponent(int base, int power)
{
int result = base;
for(int i=0; i %26lt; power; i++)
result *= base;
return result;
}
this function takes in two values, the base of the number and the power of the number, then it returns a result after the algorithm.
i hope this helps, the easiest way to understand this is through practice. lots of it.
Reply:hi,
Many functions are already available to use. Those functions are available in header files (.h) those are included in ur programe through the statement
#include%26lt;some header file name%26gt; e.g.,
#include%26lt;stdio.h%26gt; or #include%26lt;conio.h%26gt;
But still u have to define ur own functions for specific needs of ur programme. These functions perform the functions these are designed for. e.g. to add two integers u write a function as follows:
int add(int , int ); //this is prototype of the function. It tells this function will take 2 integers with it and will bring the sum of these 2 integers.
int add(int x, int y)
{
return x+y;
}
In other words, this add(int x,int y) may not be available in any of the header files, so u have to design ur own function.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment