Printing out something from a function

This is a discussion on Printing out something from a function within the C Programming forums, part of the General Programming Boards category; Hi I am a newbie to C. I have a function which search's through numbers. How can i output one ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Printing out something from a function

    Hi

    I am a newbie to C. I have a function which search's through numbers. How can i output one of those numbers? I know you have to use printf and place that in the main part of the program. When i was trying to do that it says everything is undefined because it is all declared in the function.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    paste the code!

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    you need to start C from the beginning...can't build a house from the roof downwards. pm me for a link to a good book
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by codeprada View Post
    you need to start C from the beginning...can't build a house from the roof downwards. pm me for a link to a good book
    Or he could just look in the book recommendations listed at the top of this section.

    C Book Recommendations

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tomelk31 View Post
    Hi

    I am a newbie to C. I have a function which search's through numbers. How can i output one of those numbers? I know you have to use printf and place that in the main part of the program. When i was trying to do that it says everything is undefined because it is all declared in the function.

    Any ideas?
    So... put your printf in the function.

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by tomelk31 View Post
    Hi

    I am a newbie to C. I have a function which search's through numbers. How can i output one of those numbers? I know you have to use printf and place that in the main part of the program. When i was trying to do that it says everything is undefined because it is all declared in the function.

    Any ideas?
    Functions can call functions.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    here is the code

    Code:
    void sort(float* array, int length)
    {
    . . int i, changed;
    . . float temp;
    . . changed = 1; // assume we changed something
    . . while(changed) // continue looping until we change nothing
    . . {
    . . . . changed = 0; // set the changed flag to nothing
    . . . . for(i = 0; i < length-1; i++) // loop from the first to the second-to-last
    . . . . {
    . . . . . . if(array[i] > array[i+1]) // if this one is bigger than the one in front of it
    . . . . . . { // swap them
    . . . . . . . . changed = 1; // make sure we know we changed something
    . . . . . . . . temp = array[i];
    . . . . . . . . array[i] = array[i+1];
    . . . . . . . . array[i+1] = temp;
    . . . . . . } // end if swap
    . . . . } // end for loop
    . . // if we were able to get this far without changing anything, it must have
    . . // been in the right order for every float!
    . . } // end while
    } // end function void sort(float*,int)

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    From your original post you said you had a search function... what you posted is a sort, not a search.
    And it's not a very good sort at that.

    So exactly what do you want to print from it that you can't get directly from the array?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. 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
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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