Adblock breaks this site

Functions Help

Discussion in 'Programming General' started by Linux, Aug 14, 2009.

  1. Linux

    Linux Forum Addict
    Banned

    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0
    Functions Help

    I am confused with functions
    I do not get the type name, parameters, and returning
    type name ( parameter1, parameter2, ...)
    please explain how this program works as simplest as possible focusing on what I said above

    Code:
    // function example
    #include <iostream>
    using namespace std;
    
    int addition (int a, int b)
    {
      int r;
      r=a+b;
      return (r);
    }
    
    int main ()
    {
      int z;
      z = addition (5,3);
      cout << "The result is " << z;
      return 0;
    }
    
     
  2. tofurocks

    tofurocks Iloveroy

    Joined:
    Nov 8, 2008
    Posts:
    2,344
    Referrals:
    2
    Sythe Gold:
    0
    Functions Help

    // function example
    #include <iostream>
    using namespace std;

    int addition (int a, int b) //This makes a new function. The int means that the overall function is an integer (basically a number). Addition is the name. It can be anything. The brackets tell you what you can tell the function to do, so this lets you put 2 numbers (integers) in. A and B are just titles.
    { //These 2 {} curly braces tell you what the fuction does. It makes a new number called r, and then it makes r = a and b together. Return means that Addition() (the function) will equal r.
    int r;
    r=a+b;
    return (r);
    }


    //So to wrap it up:
    If you write Addition(1,1) then it will turn a and b into 1s.
    Then, when it goes through the function, it will make r be a + b.
    It will then return r, so if you said
    cout << addition(1,1);
    then it would say 2, because it returns r, which is 2.
    Return just means the number, letter, or string which the function eg. addition() will equal when used.



    Tried to put it in my own words and reccur concepts, and no code tags because it makes it too hard to read.
     
  3. Linux

    Linux Forum Addict
    Banned

    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0
    Functions Help

    I kind of understand what you are saying, mainly confused on the return part
    anyone else have input?
     
  4. Linux

    Linux Forum Addict
    Banned

    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0
    Functions Help

    int addition (int a, int b)
    we are creating a function that will return an integer and creates two local variables called a and b

    we create an integer r and make it = a+b
    now we return(r) which Is what I do not get
     
  5. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Functions Help

    Return means that it returns a value.... Meaning... You can substitute the method in as a variable for that type. I don't know C or i'd give you an example... I'd say read up on methods that do not return values first.... >_>. unless they have to return something in c++... I dunno.
     
  6. Deacon Frost

    Deacon Frost Grand Master
    Banned

    Joined:
    Jan 30, 2007
    Posts:
    2,905
    Referrals:
    3
    Sythe Gold:
    57
    Functions Help

    It's really quite simple :). I'll explain it using PHP, but php and C are practically the same.

    Functions contain bits of code, this code is only executed when the function is called.

    Lets say you have a function like so:

    Code:
    function say_hello($firstname, $lastname) {
     echo "Hello, " . $firstname . "" . $lastname . "!";
    }
    
    That, by itself, will do nothing. This part:

    Code:
    say_hello($firstname, $lastname)
    is the function's call. The $firstname, $lastname are simple parameter variables that can be used inside of the function. That's it.

    So if you did something like:

    Code:
    say_hello(Deacon, Frost);
    
    You would see: "Hello, Deacon Frost!" output (in php, of course).

    Return, is practically the same thing, except it's used a bit differently.

    Lets say you have the following function:

    Code:
    function add($x,$y)
    {
    $total=$x+$y;
    return $total;
    }
    
    All that is doing is taking the first parameter (x), and adding it to the second parameter (y), and storing it in a variable (total). It is then telling the function that the value of itself is total (through the return method.)

    So, for example:

    Code:
    echo "1 + 5 = " . add(1,5);
    
    The first parameter is 1, and the second parameter is 5. Those two parameters are added, and the value of add(1,5) is returned as 6.


    Hope that helped :).


    Mind you, this is in PHP, but despite the few layout pieces, and commands, C, and PHP, are practically the same in syntax.
     
  7. Linux

    Linux Forum Addict
    Banned

    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0
    Functions Help

    Thanks i'm pretty sure I understand now
     
  8. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Functions Help

    It's the same thing as a mathematical function, i.e. f(x) = 3x + 4;

    A general rule of thumb: Don't do methods like what you've done. Declare the function, and write it after the main method. The main method should always come first for readability.

    for example.

    Code:
    #include <cstdio.h>
    // Function declaration, this should always be in order so you can find the functions easily.
    int add(int a, int b);
    
    // Main method.
    int main(int argc, char* argv[])
    {
        // Use the add() function to add 3 and 6 together, and print the result to the console with printf().
        printf("%i", add(3, 6));
        
        return 0;
    }
    
    // The actual add() function.
    int add(int a, int b)
    {
        // Return the sum of the arguments.
        return a + b;
    }
     
  9. Linux

    Linux Forum Addict
    Banned

    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0
    Functions Help

    kind of helps, pretty sure I understand now
     
  10. new user 1000

    new user 1000 Active Member
    Banned

    Joined:
    Jul 13, 2009
    Posts:
    211
    Referrals:
    0
    Sythe Gold:
    0
    Functions Help

    lets say u have

    function a( b, c)

    b and c go into function a. function a returns a value.

    so.. if u have x = a(2,3);

    x will equal the value of a when u put 2 and 3 into it.
     
< Need Quick Help | [NEWB FRIENDLY] Simple Hours to Minutes Converter. >


 
 
Adblock breaks this site