Archived

This topic is now archived and is closed to further replies.

BattleGuard

functions???

Recommended Posts

BattleGuard    122
Hi, Does anybody have a good tutorial for learning about functions in C++???? I tried a tutorial on www.cprogramming.com but it is the confusing, too confusing, at least for me. If you want to just read the tutorial and tell me what it means here''s the link: http://www.cprogramming.com/tutorial/lesson4.html Please help me!!! BattleGuard Whenever I try to find a better signature than this... Well, I can''t... This is it, Sorry...

Share this post


Link to post
Share on other sites
Plasmadog    205
There''s not much in that "tutorial". Perhaps you should post some specific questions about the bits that are confusing you.

Share this post


Link to post
Share on other sites
BattleGuard    122
Its kinda wide as a topic but how exactly do you declare a function, and use it, the example used in the tutorial has the function "mult(x, y)"... Can you help me...

BattleGuard

Whenever I try to find a better signature than this... Well, I can''t... This is it, Sorry...

Share this post


Link to post
Share on other sites
kordova    140
one example

    
#include <iostream>

//prototype for the function so you can call

//it later on before it is defined

//here it takes two parameters or variables

int mult(int x, int y);

int main() {
int i = 2, j = 3, z = 0;
//send i as the "x" and j as the "y"

//to the mult function returning an int, z

//see later definition

z= mult(i,j);
std::cout<<z<<std::endl;
return 0;
{


//definition of mult function

int mult(int x, int y) {
//return value of type int (as per "int" mult())

return x*y;
}

Not tested but try it.

[edited by - kordova on May 26, 2003 10:29:23 PM]

Share this post


Link to post
Share on other sites
Plasmadog    205
Well, there are just a few essentials. You need to tell the compiler at least three things: what your function will be called, what type of value it returns, and what types of arguments it will take. The mult() example takes two integers, x and y, as arguments, and returns the result as an integer.

1 int mult(int x, int y);
2 ...
3 int mult(int x, int y)
4 {
5 return x*y;
6 }


Line 1 is the function''s prototype. It describes those three essentials. Actually, the x and y aren''t strictly neccessary here, it could just say
int mult (int, int);
Lines 3-6 are the function definition, which describes what the function actually does. Line 5 does the multiplication and causes the function to return the result.
Does that help?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Ok here''s another example, let''s say you want to have a function that adds two numbers...


  
#include <iostream>


int Add(int x,int y);

/*
the int in front of the function means you want the
function to return an integer
x, and y are the variables you pass to the function

if you wouldn''t want the function to return anything you would
just put void instead of int
*/


int main()
{
//you declare the variables you want to add

int a = 5;
int b = 5;
//r equals a+b

int r = Add(a,b); //now r equals a+b

cout <<r<<endl;

return 0;
}

int Add(int x,int y)
{
return x+y;
}


Ok I hope that helped out a little but you probably want to search on google or something for a good tutorial on functions.

Share this post


Link to post
Share on other sites
BattleGuard    122
Yeah thanx, you all I think I got it now...

BattleGuard

Whenever I try to find a better signature than this... Well, I can''t... This is it, Sorry...

Share this post


Link to post
Share on other sites
Dean Harding    546
quote:
From that "Tutorial"
Cout is an example of a function.



Eww! That''s the third sentance in that tutorial. I don''t usually consider myself pedantic, but I just couldn''t read past that totally crazy statement.

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
BattleGuard    122
quote:
Original post by Anonymous Poster
Ok here's another example, let's say you want to have a function that adds two numbers...



        
#include <iostream>


int Add(int x,int y);

/*
the int in front of the function means you want the
function to return an integer
x, and y are the variables you pass to the function

if you wouldn't want the function to return anything you would
just put void instead of int
*/


int main()
{
//you declare the variables you want to add

int a = 5;
int b = 5;
//r equals a+b

int r = Add(a,b); //now r equals a+b

cout <<r<<endl;

return 0;
}

int Add(int x,int y)
{
return x+y;
}


Ok I hope that helped out a little but you probably want to search on google or something for a good tutorial on functions.


Hi, even though I have a good grip on functions, I wanted to know, since I'm a beginner, what this part in the cout statement does:

<< endl ;

What does the endl do??? Is it something like endline or something???

BattleGuard

Whenever I try to find a better signature than this... Well, I can't... This is it, Sorry...


[edited by - BattleGuard on May 27, 2003 6:34:32 AM]

[edited by - BattleGuard on May 27, 2003 6:35:31 AM]

Share this post


Link to post
Share on other sites
JonnyQuest    331
Yes, endl ends the line. Alternatively you could use "\n", which is useful if you''re already outputting a string anyway.

- JQ
#define NULL 1

Share this post


Link to post
Share on other sites
EvilCrap    134
first of all, "cout" is not a function, it is an object name.

the leftshift operator, "<<", is actually the function here. when you write,

cout << "hi" << endl;

you are actually using a stream operator, which you will learn about when you study objects, the prefix version looks like this:

cout.operator<< ("hi"); //call the output function and send "hi"
cout.operator<< (endl); //call the output function and send "\n"


got it?

objects, as you will learn, can have their own operators defined, like <<, +, -, [], (), etc.

Share this post


Link to post
Share on other sites
Annatar    122
I, too, am new to C++ and have a question about functions. I don''t understand why the definition of the function comes after the actual program. Any clarification would be much appreciated.

Share this post


Link to post
Share on other sites
BattleGuard    122
Actually the definition should have all come later... Everything, only that the compiler can know that the function exists so it doesn''t give an error something like: "Undefined function" or something...

BattleGuard

Whenever I try to find a better signature than this... Well, I can''t... This is it, Sorry...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Annatar
I, too, am new to C++ and have a question about functions. I don''t understand why the definition of the function comes after the actual program. Any clarification would be much appreciated.


It doesn''t matter when the definition of the function takes place in code as long as you have the prototype first. This way the compiler knows that there will be a function called Foo(), with the appropriate arguements and return. Hope that helps.

Share this post


Link to post
Share on other sites