Thread: is this how a function is suppose to work?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    51

    is this how a function is suppose to work?

    I have something like this,


    Code:
    
    int main(void)
    {
    int x[100];
    
    // loop and scanf to get user input for array
    
    }
    
    
    int calculate( x[100] )
    {
    
    //variable passed from main function into this function
    //bunch of code here to do calculation
    
        return (x[100]);
    
    }



    basically all I'm trying to do it pass a variable from main function to calculation function.

    I'm reading my text....and I kinda think I got the concept wrong...

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    int main(void)
    {
    int x[100];
    
    // loop and scanf to get user input for array
    
    }
    
    
    int calculate( x[100] )
    {
    
    //variable passed from main function into this function
    //bunch of code here to do calculation
    
        return (x[100]);
    
    }
    Luckily your compiler should warn you about that. You are somewhat on the right track already. Just call the function like calculate(x);

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by master5001 View Post
    Code:
    int main(void)
    {
    int x[100];
    
    // loop and scanf to get user input for array
    
    }
    
    
    int calculate( x[100] )
    {
    
    //variable passed from main function into this function
    //bunch of code here to do calculation
    
        return (x[100]);
    
    }
    Luckily your compiler should warn you about that. You are somewhat on the right track already. Just call the function like calculate(x);

    why did you highlight the 100?

    I don't know what you mean.....


    I been getting a bunch of compiler errors....but the program itself works flawlessly.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As he said in the text right below the program, it needs to be removed from your program.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ktran03 View Post
    I'm reading my text....and I kinda think I got the concept wrong...
    Then you probably did.

    The type of the parameter 'x' in calculate is missing, it should be int by the looks of it.
    Returning x[100] is a buffer overflow because arrays start at zero so you're actually returning item 101 of an array with only 100 items. That's why it is red.
    The program possibly works flawlessly because you don't call the function with the buffer-overrun.
    If you want to demonstrate the concept of functions, then you really need to actually call the function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ktran03 View Post
    I been getting a bunch of compiler errors....but the program itself works flawlessly.
    How can you run a program that has compiler errors?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    int calculate( int x[100] )
    Another problem is that you didn't specify a type for the parameter.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by QuantumPete View Post
    How can you run a program that has compiler errors?

    QuantumPete
    dam Xcode....


    I thought I was running it...but in fact, the terminal that ran ...ran the code I had working before I split my program into functions.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    I got it working now....some of it atleast.

    scrap what I had in the first post, I was tryng to summarize the code so it isn't so long

    this is what I should have posted in the beginning...

    Code:
    
    
    int main(void)
    {
    int x[100];
    
    // loop and scanf to get user input for array
    
    // ????? what do I put here to get output from the checker(function 2) function?
    
    }
    
    
    int calculate( x[100] )   //function 1
    {
    
    //variable passed from main function into this function
    //using this array, do a bunch of calculations
    
    
    tempvar =  x[25] * 10
    
    
        return (tempvar);
    }
    
    
    int checker()  //function 2
    {
    
    //I want to receive tempvar from the calculate function into this function
    //I also need the x[100] also
    
    
    }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by Elysia View Post

    I hate to by the guy asking for help, and not bothering to search for answers myself....

    but....I have three reference books, and I still can't figure this out. *very close to calling it quits


    I'm gonna go through the link, hopefully I figure this out....thnkx.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you can't figure it out even with the link just keep adding to the thread. The whole point of a forum is for the extra level of interaction and the ability to simply "still not get it." We are the absolute last line of defense that lies between where you are now and giving up.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    ^your absolutly right

    I'm just a little frustrated....


    I decided I have the whole concept wrong, and I am gonna start from scratch.



    now here, scratching my head once again:

    Code:
    printf("The entered isbn 10th digit is %d\n" , isbn[9]);
    
    ldigit = 11 - (sum)%11;
    
    printf("The entered isbn 10th digit is %d\n" , isbn[9]);
    it seams impossible, but the first printf statement prints a number different from the second printf(3rd LOC). How can that middle line have any influence in the array?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ktran03 View Post
    ^your absolutly right

    I'm just a little frustrated....


    I decided I have the whole concept wrong, and I am gonna start from scratch.



    now here, scratching my head once again:

    Code:
    printf("The entered isbn 10th digit is %d\n" , isbn[9]);
    
    ldigit = 11 - (sum)%11;
    
    printf("The entered isbn 10th digit is %d\n" , isbn[9]);
    it seams impossible, but the first printf statement prints a number different from the second printf(3rd LOC). How can that middle line have any influence in the array?
    How would we know? Maybe isbn[9] doesn't exist.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Re: your original question...
    When you declare and define a function, you specify its return type, as well as the types of the parameters to the function, to which you can pass arguments.
    return_type function_name(parameter_type parameter_name, ...)

    To return something from a function, you would use the "return" statement.
    T get something returned from a function, you would assign it to a variable:

    my_var = myfunctioncall(...);

    Now my_var will contain whatever myfunctioncall returned.
    And you also know that you can pass argument to a function:

    myfunctioncall(arg1, arg2);

    The arguments you pass shall match the type you decided in the declaration. So if you know you are going to pass a char array to it, then you declare the type of one parameter to such a type to be able to handle a char array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM