Functions

This is a discussion on Functions within the C++ Programming forums, part of the General Programming Boards category; Im C++ newbie im started understanding if commands,loops, and strings and even made a mini project by maiking a basic ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    22

    Functions

    Im C++ newbie im started understanding if commands,loops, and strings and even made a mini project by maiking a basic math quiz. the thing i wanna hit next is understandign functions. the tutorial is too plain can ne1 give a better example of when and how to use functions and explain it?

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Let me show you, say, a function that squares it's input:
    Code:
    long square (long x)
    {
         return x*x;
    }
    This function takes one argument, which is named x and is of type long:
    Code:
    long square (long x)
    It also returns a long:
    Code:
    long square (long x)
    This is the line that returns the value:
    Code:
    return x*x;
    That's it! While this is a highly simplified example, it illustrates the basic concepts of functions.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    22
    thanks

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    22
    but how would you use that square function in a program i tryed but got confused by ho to declare x

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Sorry. Look at this:
    Code:
    long thenum = 3;     //This is our number.
    thenum = square(thenum);     //This gets an argument of thenum and returns it's result into thenum.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    22
    Ok that explains it better but at the top I have to declare the function like int ( x and whatever
    how would I declare the function by using only one variable?

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    22
    so fricking confusedy if im being a pain
    Code:
    #include <iostream>
    
    using namespace std;
    
    int square x;
    
    int main()
    {
      int x;
      
      
      cout<<"Please input the number to be squared: ";
      cin>> x ;
      cin.ignore();
      cout<<"The square is  <<"\n";
      cin.get();
    }
    
    long square (long x)
    {
         square x*x;
    }
    I know this is completely wrong but this is my futile attempt to apply what you just told me into a program. can u tell me wat i did wrong?

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Just about everything. What you need is a good introductory book. See the sticky thread.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    >int square x;

    I think you meant that to be a function prototype. What you have done is created a
    global variable.

    >cin.ignore();

    You do not need that in there at all

    You need to pass the argument the function for it to calculate the result.

    Code:
    square ( x );
    Oh, and you declared the prototype ( global variable ) of type int, when the function
    square is defined as long. A function header and a function prototype must match in return types apart from function overloading - which is a different matter all togther. There are a few more errors too.

    As cornbee said, it is best you read a tutorial or get a good beginners book. Check out the book recomendations on this site for some advise on what one(s) to get.
    I'm just trying to be a better person - My Name Is Earl

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Read my posts (and forward declare as long square (long x);)
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

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