having problems understanding functions in C, anyone care to explain me? =)

Sharky Forums


Results 1 to 10 of 10

Thread: having problems understanding functions in C, anyone care to explain me? =)

  1. #1
    The Medieval Mod freedon's Avatar
    Join Date
    Aug 2001
    Location
    Currently: Dallas, US. Originally: Monterrey, Mexico
    Posts
    8,669

    Smile having problems understanding functions in C, anyone care to explain me? =)

    in my C class for engineering, i'm having problems understanding how to make/put functions on the program

    these are my teachers notes
    http://ringer.cs.utsa.edu/~mmaltrud/.../functions.htm

    i already had 2 homeworks using fucntions and well.. the first one i got a 50 caz i did it wrong and in the second one a 70.. and i have a test on april 19th so i want to learn how functions are created

    i kind of have the idea of what the idea is, but when doing a 'recall funtion' or 'return function' i just don't get i do't know where to put them or what variables to put

    care anyone to spend time in teaching me
    thanks
    "Est Solarus Oth Mithas"
    My Honor is My Life


    (\__/)
    (='.'=)This is Bunny. Copy and paste bunny into your
    (")_(")signature to help him gain world domination

  2. #2
    Catfish arcane_III's Avatar
    Join Date
    Apr 2002
    Location
    East Coast, USA
    Posts
    151
    Your best bet is to look at over the notes well and check out the case study examples.

    "return fuctions" as you call them are fuctions that return a value, they are functions that are not declared
    void function name(parameters)

    some examples of fuctions that have a return value are:
    x = pow(x,2)
    x = sqrt(y)

    etc...

    if you are familar with VB which has both procedures and functions then you can relate a void function_name( parameters )
    declaration with a procedure(SUB), and relate any function that has a return value to the VB function.

    In general functions come in handy when you want to use a function in place of a variable or in the place of a parameter.
    Example:
    Suppose you want to find the square root of 2, and print the result.

    printf( sqrt(2.0) );
    would accomplish this.

    In this example printf is declared:
    void printf( ... ) and does not return anything.
    but sqrt() has a return value.

    This return value is then passed to the printf function.


    Well. Hope that helped a little, and didn't just confuse you more.

  3. #3
    The Medieval Mod freedon's Avatar
    Join Date
    Aug 2001
    Location
    Currently: Dallas, US. Originally: Monterrey, Mexico
    Posts
    8,669
    i kindof did understand

    so how about the variables and parameters that start in the begining of each part?

    for example

    main
    {..
    .
    .
    .
    .}

    num(int..., int ...)
    {
    .
    .
    .
    .}


    does that num(int, int) share the same way as you explained ?
    "Est Solarus Oth Mithas"
    My Honor is My Life


    (\__/)
    (='.'=)This is Bunny. Copy and paste bunny into your
    (")_(")signature to help him gain world domination

  4. #4
    Reef Shark mefisto3's Avatar
    Join Date
    Jul 2001
    Location
    Melbourne, Vic, Australia
    Posts
    429
    it is very simple

    functions basically perform some operations (do something, whether it is a calculation or printing to screen or anything else). they can return a value but do not have to. they can also take parameters but dont have to.

    for example (using Java syntax):
    void print(String text)
    {
    System.out.println(text)
    }
    takes String text as parameter and outputs it to screen, it does not return anything (hence void infront of the function). can be called like this:
    print("blah blah blah");

    int add(int a, int b)
    {
    return a + b;
    }
    takes two integers as parameters (a and b) adds them together and returns the result (as an integer too). can be called like this:
    int x = add(1, 2);

    int random()
    {
    // generate random number x (no code included)
    return x;
    }
    takes no parameters and returns a random number. can be called like this:
    int myRandomNo = random();

    Hope it makes it easier for u.

    If u r having trouble with something as simple as functions then maybe programming is not for u.

  5. #5
    The Medieval Mod freedon's Avatar
    Join Date
    Aug 2001
    Location
    Currently: Dallas, US. Originally: Monterrey, Mexico
    Posts
    8,669
    yes i understod better

    i have to take at least one class of C
    i don't hate it, but i'm just having problems



    Originally posted by mefisto3

    If u r having trouble with something as simple as functions then maybe programming is not for u.
    i think that to fail right now is to improve in the future
    (like i said, i study engeenering) not computer science.. now that would make me worry
    "Est Solarus Oth Mithas"
    My Honor is My Life


    (\__/)
    (='.'=)This is Bunny. Copy and paste bunny into your
    (")_(")signature to help him gain world domination

  6. #6
    Texan Dragon Moderator Galen of Edgewood's Avatar
    Join Date
    Sep 2000
    Location
    Ft Irwin, CA
    Posts
    5,602
    Originally posted by freedonX
    does that num(int, int) share the same way as you explained ?
    If I understand what you are asking, then yes.

    Take this function as an example:

    Code:
    int numSum (int a, int b) {
         /* This function returns that sum of a + b */
         int sum;
         sum = a + b;
         return sum;
    }
    Simple enough of a function, right? (No, I don't personally code like that, but it'd probably confuse you if I coded the way that I code sometimes. )

    Anyways.... Say you have a value that is to be the sum of two integers, and we'll call it sumOfInts. (Yes, I'm highly original...) You want sumOfInts to be equal to intA and intB. To do that, using the function numSum, you'd use the following syntax:

    Code:
    sumOfInts = numSum(intA, intB);
    What happens is that 'intA' and 'intB' are sent into the function. The functions adds 'intA'(now known as 'a' inside the function) and 'intB' (now known as 'b' inside the function) and puts that value into the varible 'sum', which was declared inside the function. The function is exited at return and 'sumOfInts' is set to the value of what 'sum'.

    I hope that helps you... If not, ask again in a differnt way and hopefully we'll clarify some more.
    Dragon of the OC Crusaders

    Break the rules and you're snack food for this dragon...

  7. #7
    Catfish arcane_III's Avatar
    Join Date
    Apr 2002
    Location
    East Coast, USA
    Posts
    151
    Yeah most engineering programs's make you take a CS class or two.
    Some of them even have you learn a little assembly language and build a old computer (8086) or something like that.

  8. #8
    Texan Dragon Moderator Galen of Edgewood's Avatar
    Join Date
    Sep 2000
    Location
    Ft Irwin, CA
    Posts
    5,602
    Originally posted by arcane_III
    Yeah most engineering programs's make you take a CS class or two.
    Some of them even have you learn a little assembly language and build a old computer (8086) or something like that.
    I had to take assembly for an 8086 for my CS degree... Never had to build one though! I did do several interesting things with IC's though. Fun things, though messy as all get out with the wires all over the derned place.
    Dragon of the OC Crusaders

    Break the rules and you're snack food for this dragon...

  9. #9
    Catfish arcane_III's Avatar
    Join Date
    Apr 2002
    Location
    East Coast, USA
    Posts
    151
    Yeah, assembly was my favorite CS class. I kinda wish I had completed my Engineering minor and had gotten to do all that cool hardware stuff.

  10. #10
    The Medieval Mod freedon's Avatar
    Join Date
    Aug 2001
    Location
    Currently: Dallas, US. Originally: Monterrey, Mexico
    Posts
    8,669
    thanks everybody!
    i have a homework due tomorow, so i give it my best shot in trying yo do it right
    "Est Solarus Oth Mithas"
    My Honor is My Life


    (\__/)
    (='.'=)This is Bunny. Copy and paste bunny into your
    (")_(")signature to help him gain world domination

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •