Jump to content
  • Advertisement

Archived

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

BattleGuard

functions???

This topic is 5532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!