I dont get functions

This is a discussion on I dont get functions within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been trying to get a simple fuction to work for ages, i understand what functions are and what ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    49

    I dont get functions

    Hi, I've been trying to get a simple fuction to work for ages, i understand what functions are and what they do i just dont know how to make the give and return values. I've read the chapter on functions over and over plus some tuts on the net but i still dont get it.
    I know this is a bit much to ask but could someone please explain exactly how fuctions give and take data.

    Heres a peice of something i'm trying to do
    Code:
    #include <iostream>
    
    int half(int a);
    
    int main()
    {
    	int a;
    	std::cout<<"Enter a number: ";
    	std::cin>>a;
    	half(int a);
    	std::cout<<"The number is now " <<a;
    	return 0;
    }
    
    inline int half(int a)
    {
    	a=-5;
    	return a;
    }
    this code gives me these errors

    error C2144: syntax error : missing ')' before type 'int'
    error C2660: 'half' : function does not take 0 parameters
    error C2059: syntax error : ')'

    i've tried just about everything i can think of but i just dont get it
    Thanks

  2. #2
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    when you pass a variable to a function within the body of main, you dont have to define that variable again.

    IE, instead of passing "int a" to half, just pass "a"

    the function prototype and the function definitions just show what type of variable can be passed to that function. you could define your function like this:

    int half ( int number );

    and then pass it something else:

    half( a );

    but within the function body, you use whatever is in your function prototype:

    int half ( int number )
    {
    number =number / 2;
    return number;
    }
    Last edited by ...; 07-31-2003 at 01:00 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Thanks ... , but that doest seem to work

    example
    Code:
    #include <iostream>
    
    int half(int letter);
    
    int main()
    {
    	int a;
    	std::cout<<"Enter a number: ";
    	std::cin>>a;
    	half(a);
    	std::cout<<"The number is now " <<a;
    	return 0;
    }
    
    inline int half(int letter)
    {
    	letter = / 2;
    	return letter;
    }
    not only do i get this error
    error C2059: syntax error : '/'

    so i change the / to - just for simplicity
    then it compile fine but this is what the program does

    enter a number: 50
    the number is now 50

    do u think it might be my compiler? i'm using msvc++6

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Maybe your book sucks!

    You might take a look as some other books to see if they explain things more clearly.

    The only beginning book I have is "Teach Yourself C++ in 21 Days" by Jesse Liberty. I don't have it with me... and I don't remember the specific explainations of functions... but in general, the book is very clearly written.

    Functions CAN be confusing at first. Here's something that had me confused: The variable names passed to the function can be different from the variable names inside the function.

    For example, this is OK: (but incomplete)
    Code:
    {
         /...
         int X = 10; // Length
         int Y = 15; // Width 
    
          A = Area(X, Y);
          /...
    }
    
    int Area(int Length, int Width)
    {
         return Length * Width;
    }
    This works because functions (normally) "pass by value". That means that the values10 and 15 are what gets passed-in.
    Last edited by DougDbug; 07-31-2003 at 01:16 PM.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Well its starting to make sense now but its just not compiling and when it does the program doesnt work

    The book i'm using is called c++ interactive course which was recomended by lots of ppl at gamedev.net

  6. #6
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    yes, my apologies, i rushed through my reply.

    do what salem said and your program will run fine...

    scope is very important when youre thinking about functions... whenever you have a set of brackets (such as a loop, a function, or main) any variables you define inside of those brackets only has scope inside of those brackets, meaning no other part of the program can see that variable. when you pass a variable to a function, you arent passing that exact variable, but merely the value of that variable, and then the function makes a copy of it to modify.

    when a function returns a value, you must have a variable to "catch" that value, because the copy that the function made dies when the function ends.

    like so:

    a = half(a);
    I came up with a cool phrase to put down here, but i forgot it...

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This should be correct.
    Code:
    #include <iostream>
    
    int half(int letter);
    
    int main()
    {
    	int a;
    	std::cout<<"Enter a number: ";
    	std::cin>>a;
    	a = half(a); // one correction here
    	std::cout<<"The number is now " << a << endl;
    	system("PAUSE");
    	return 0;
    }
    
    inline int half(int letter)
    {
    	letter /= 2; // another correction here
    	return letter;
    }

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Ahhh, it all makes sense now, sorry i dont know how i missed salems post
    Well thanks heaps for that

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Notice what ... did with your return-calculation statement:

    The /= stuff is shorthand...

    This is the same thing to the compiler, and easier for humans to understand!

    number = number / 2;

    In C++ (and in most other programming languages) the following will add 10 to X:

    X = X + 10;

    Note that this is NOT an equation... it makes no sense in math. It is an assignment operation. It means "Assign a new value to X, which is 10 more than the current value."

    The shorthand for this in C++ is:
    X += 10;

    In fact, you will see the += shorthand used in almost all "real" programs. But, to the compiler, they are EXACTLY the same.
    Last edited by DougDbug; 07-31-2003 at 01:52 PM.

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Thanks for the tip DougDbug

    you guys rule

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21