functions???
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...
There''s not much in that "tutorial". Perhaps you should post some specific questions about the bits that are confusing you.
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...
BattleGuard
Whenever I try to find a better signature than this... Well, I can''t... This is it, Sorry...
one example
Not tested but try it.
[edited by - kordova on May 26, 2003 10:29:23 PM]
#include <iostream>//prototype for the function so you can call //it later on before it is defined//here it takes two parameters or variablesint 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 functionint 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]
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.
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?
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?
Ok here''s another example, let''s say you want to have a function that adds two numbers...
Ok I hope that helped out a little but you probably want to search on google or something for a good tutorial on functions.
#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.
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...
BattleGuard
Whenever I try to find a better signature than this... Well, I can''t... This is it, Sorry...
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.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]
Yes, endl ends the line. Alternatively you could use "\n", which is useful if you''re already outputting a string anyway.
- JQ
- JQ
#define NULL 1
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement