Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Functions and doing a function call

Status
Not open for further replies.

menace212

Programmer
Jul 11, 2003
144
US
Can some give me an example of calling a function and a function call for int main()....I used functions before and I lost track of how to call it in int main()
 
Code:
int f(); //prototype (or prototype and definition
// must be before call 

int main()//main function
{
   f(); //call to f
   return main(); //infinte recurssion -- don't do this!
//without a conditional return
}

int f() //definition of function f
{
  //do something useful
  return 5;
}
The compiler needs to know a function exsists before it is called, it doesn't need to know what it does yet, so prototypes are used (typically at head of a file or in a header file. The definition has a matching signature (exsact match, with the exception of option paramaters (which can only be given once (with definition?). I've maid main a (infinate) recursive function, thus the program will go on forever (untill the stack overflows), doing nothing.
 
Do I need to declare these functions at the beginning of the program before main().for example


include <such and such>
.
.
.
.
int f();

int main()

.
.
f();
.
.

int f()
{
do something
return 0;
}



 
The includes have to come first, and the funtion must be protyped before the function is called.
Code:
#include <iostream> //First any includes
using namespace std; //namespaces if used
//prototypes

float sqr(float); 
float callF(float);

int main()
{
  float x = 9.8;
  cout << sqr(3.5) <<endl; //prints the return 
  //value of sqr(3.5)
  cout << callF(3) <<endl; //prints 3 after making the
  //function call
  float z = sqr(x); //x isn't changed...  
  return 0;
}

void F() //Note, no prototype, 
//but it's leagal because it hasn't been called.
{
  cout << &quot;Hi&quot; <<endl;
}

float sqr(float f) //prototyped abaove
{
   return f * f;
}

float callF(float f) //prototyped above
{
  F();
  return f;
}
Most functions have two parts the protype which is just the signature in the form of

<return type> functionName( [paramteres [ = defualt value]][...]);

The return type is just the type (int, float, vector, ect.) the function name can be anything that is alphanumaric or underscores starting with an alpha char or underscore (no numbers, or funny symbols). The parameter list is just a list of argumetents (optionally named here - but you don't need to) and then terminated with a semicolon.
Code:
float sqr(float);
So, this function takes a float, returns a float and is named sqr. As the name implies we write it to return (by value) the square of the value passed (passed by value). When passing or returning by value a copy of the object is created, and the original isn't modified.

If I get bored (with the stuff I got to do) I'll copy this into my first FAQ, and add to it to cover the bases. For now, I gotta run.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top