English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

please show example n explanation...thanks...

2007-09-09 20:18:07 · 2 answers · asked by ct k 2 in Computers & Internet Programming & Design

2 answers

a function basically has two parts to it

function_name( {

//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

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 < 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.

2007-09-09 20:53:36 · answer #1 · answered by xytose 3 · 0 0

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 e.g.,
#include or #include

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.

2007-09-09 20:53:04 · answer #2 · answered by iqbal 4 · 0 0

fedest.com, questions and answers