Advertisement

oop and funcshon help

Started by January 06, 2003 10:25 PM
6 comments, last by headshot 21 years, 10 months ago
ok, i got up to oop in c++ to day, but while i under stand the idea behind oop, the sintax and geting it to work is uterly beyond me. and simerler things apply to funcshons, i can put them together ok, but geting data betwen them and how it all works together is as clear as mud. please help a n00b out,
i am god!
i really think you should make sure you fully understand functions and other basic things before you move onto anything else..........

here is a quick explaination of how functions work

//--------------------------
int myFunc(int, int);

ok this is the function prototype, this particular one says the the function named myFunc, takes as parameters 2 integers, and will return an integar. you can give these names to remind what they are

ie int myFunc(int age, int weight);


//----------------
here is the function definition

int myFunc(int a, int b)
{
return a+b;
}

notice that as in the prototype it takes 2 integers as parameters, this time they must be named, ie. a and b so you can refer to them in the function body.

it also returns an integer, in this case its the sum of a and b.
//--------------------------

here is the function call

answer = myFunc(2,4);

this passes the values 2 and 4 to a and b.

the function then returns the sum of a and b and it is assigned to the variable answer.

here is what it would look like when all put together.


    #include <iostream>using namespace std;int myFunc(int,int); // function delaration return an int and takes 2 int parameters/arguementsint myOtherFunc(int); // different function declaration//--------------------------------------------------------------------int main(){	int answer;	answer = myFunc(2,4); // function call, arguements are 2,4 the functions return is assigned to answer	cout << answer << endl;	//cout << a; // this cannot be done because a and b are local to myFunc	cout << myOtherFunc(answer) <<endl; // this time the value of answer is passed as a parameter	// and the functions return is printed to the screen instead of being assigned to a variable	return 0;}//------function declaration-------int myFunc(int a, int b){	cout << "im myFunc\n";	return a+b;}//---end of declarationint myOtherFunc(int anyOldName){	//answer = 100; //cannot be done because answer is local to main	cout << "im myOtherFunc\n";	return anyOldName;}    


btw im a noob too so goodluck

[edited by - deal on January 6, 2003 12:18:56 AM]
Advertisement
thanks! damn, c++ is so much harder then basic. i have it now i think,

so i say, return var, and when ever i call that func i get the vallue of var.

thanlks, i am trying to take it slow, but i am not acostum to things being this hard, lol

thanks again!
i am god!
quote: Original post by headshot
thanks! damn, c++ is so much harder then basic. i have it now i think,

so i say, return var, and when ever i call that func i get the vallue of var.

thanlks, i am trying to take it slow, but i am not acostum to things being this hard, lol

thanks again!


Give it some time Headshot, after a while it will come more natural to you and you will wonder how the heck you programmed in basic.

As was mentioned above make sure you know functions VERY well as they are important in programming (break up important pieces of code to make it easier to understand). Once you get to pointers you will then become confused hehehehe
ya, i have tryed the poynters chapter like, 5 times now, if you hear something spining it is my head
i am god!
ya, i have tryed the poynters chapter like, 5 times now, if you hear something spining it is my head
i am god!
Advertisement
ok, here is my attempt at it


  // func.cpp : trying to lerrn functions//not working#include <iostream.h>#include <stdio.h>int c;int d;char* answer;int main(int c)   {		cout << "tell me a number \n";		cin >> c; 		return c;}int func(int a, int b){	if (c < 10)	{		a = 8;			b= 2;	}else 	{	a = 88;		b= 2;	}d = main()answer = a + b + d;cout << answer;return 0}//it says this line has a error?  


hu, i am shore i did this rong,
i am god!
not even sure what you were attempting

ok here some things you did wrong.

you didnt have a function delaration before main()

ie. int func(int, int);

you have declared variables that are global, generally you should declare you variables to be local, ie inside main or a function.

you seem to be trying to call the main function from a function, in an attempt to get the return value, in this case func() already has been passed c.

dont try to call main() in youre programs.

btw its a good idea to give variables a descriptive name, so youre program is easier to follow.

anyway i took a guess at what you were attempting to do


  #include <iostream.h>//#include <stdio.h> // not needed in this program//int c;	// you should avoid declaring variables here//int d;  // its best to delare them in main or in a functionint func(int); // you had no function declaration, you need a function declaration if you put youre function defination after main() or in a header file.int main(){		int c;	int someVariable;	cout << "tell me a number \n";			cin >> c;		someVariable = func(c);	cout << someVariable;	return 0;}// note that the c in func is initialized to the value of c in main// and if we were then to change the value of c in func, c in main would// be unaffected.int func(int c) // function only needs one parameter in this case{		int a;	// these variables are local to func()	int b;	if (c < 10)		{				a = 8;					b = 2;		}	else 		{			a = 88;				b = 2;		}		return a + b + c;}  


visit here for a better explaination of functions than i can give, i have only been programming a short time.

http://www.cplusplus.com/doc/tutorial/tut2-2.html

dont worry it will all click then you wont be able to beleive you were having trouble




This topic is closed to new replies.

Advertisement