Thread: Functions, do i have the wrong idea?

  1. #1
    Don't be a jerk.
    Join Date
    Jan 2013
    Location
    Snowball Arkansas
    Posts
    11

    Lightbulb Functions, do i have the wrong idea?

    Are functions not just a fancy way of initializing a variable?
    Ultimately, a function just returns its specified return type with a value to the caller.
    The entire program itself, as far as i can tell, is treated by the OS as a variable (containing the return value of main(), usually 0)
    I am well aware there are void functions, but those are 'procedures', right? To truely be a function, means to actually 'function' on some sort of data and return something when called.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ramidavis View Post
    Are functions not just a fancy way of initializing a variable?
    Ultimately, a function just returns its specified return type with a value to the caller.
    The entire program itself, as far as i can tell, is treated by the OS as a variable (containing the return value of main(), usually 0)
    I am well aware there are void functions, but those are 'procedures', right? To truely be a function, means to actually 'function' on some sort of data and return something when called.
    No, sorry. A function is a trip to the supermarket - you go, you bring stuff with you, maybe (cash), and you do SOMETHING, and then you come back. Maybe you bring back something, or maybe you found nothing you wanted today.

    Same for the program. It goes, to the function via the call, it brings zero or more parameters along with it, and it comes back to where it came from. Sometimes it returns a value, sometimes it doesn't.

    Whether the function has parameters or not, doesn't matter - exactly the same concept. Whether the function has a return value also has nothing to do with whether there was some work done inside the function.

    The answer is to program more, and think less, clearly. LOL!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ramidavis
    Are functions not just a fancy way of initializing a variable?
    Not in my opinion.

    Quote Originally Posted by ramidavis
    Ultimately, a function just returns its specified return type with a value to the caller.
    I think you mean "returns a value with its specified return type to the caller". The problem is that you are overlooking the fact that functions can have side effects, e.g., to perform input/output. Furthermore, through the use of pointers, they can change the state of the caller even without returning a value. Then, they can change global variables.

    Quote Originally Posted by ramidavis
    The entire program itself, as far as i can tell, is treated by the OS as a variable (containing the return value of main(), usually 0)
    If that were the case, then this C program is equivalent to all other C programs:
    Code:
    int main(void)
    {
        return 0;
    }
    And indeed writing a C program would be a pointless exercise since it would suffice for the OS to initialise this "variable" to 0.

    Quote Originally Posted by ramidavis
    I am well aware there are void functions, but those are 'procedures', right? To truely be a function, means to actually 'function' on some sort of data and return something when called.
    Sure, you could have such a definition of "function" versus "procedure" if you want, but that is not C terminology.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Functions are as a concept basically just a list of instructions that have a name. How do you print the string "Hello", given that we only know how to print one character? Easy:

    Print 'h'
    Print 'e'
    Print 'l'
    Print 'l'
    Print 'o'

    We can give the function a name, PrintHello, so that we don't have to write 5 instructions each time. How do we print a string, given that we can store a string character-for-character and assuming we mark the end of a string with a special marker character '\0'? This is, in a very simple sense, what printf does.

    printf("Hello, world\n");

    If you want, think of it as merely a shorthand for writing all of the individual instructions which output individual characters. Of course, once we have a set of useful functions, we can write more functions which call those functions, and so on. It means the complexity of what we can do in a practical sense increases very rapidly, only by introducing a very simple concept (a function).

    As was mentioned, "procedures" and subroutines and methods and so on is all called the same in C: "function". It's simple that way.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Take home message : Functions make your code easier to read and increase the re-usability of your code.

    I will use an example from my small experience. Print an array of N size. When I started, I didn't use functions, so every time I had to search for the code in my previous program and copy paste it to the new one. As the programs getting bigger and bigger, that was difficult, enough difficult to make me write the function from scratch. Then, I wrote the code that needs to be written for printing an array in a function, and simple reuse this function every time I need it.

    First approach
    Code:
    ...
    int main(void)
    {
           int i;
           ...
           for( i = 0 ; i < 10 ; i++)
                 printf(...);
           return 0;
    }
    Second approach
    Code:
    ...
    
    void printArray(int* array);
    
    int main(void)
    {
            ...
            printArray(array);
            return 0;
    }
    
    void printArray(int* array)
    {
         int i;
         for( i = 0 ; i < 10 ; i++)
                 printf(...);
    }
    But, as I used the function again and again, I saw that I had to change the condition of the for loop again and again, according to the needs of every program. Many times I forgot to, thus got a logical error( less elements were printed ) or I was going out of bounds (segmentation fault). Of course, I spent much time searching for the bug, which was in the function!
    So, I decided to make the function a bit more generic. I decided to pass the size of the array, as second parameter of the function and never needed to worry about that again. No errors, no time spent on debugging for the part of program that prints the elements of the array. (!) Re-use of code, through functions usually, makes you write less code from scratch and decreases the amount of code that you have to debug. The re-used code is considered to be correct,

    Third approach (I still use this function!)
    Code:
    ..
    void printArray(int* array, int N);
    
    int main(void)
    {
         ....
         printArray(array, 50);
         return 0;
    }
    
    void printArray(int* array, int N)
    {
          int i;
          for(i = 0 ; i < N ; i++)
               printf(..);
    }
    You can make the function more generic than this, but I think that for usual use, this is more than fine

    After this, I really understood how valuable this function was, so I decided to make other too. After some functions for filling/printing arrays, I decided to make one function for dynamically allocate a 2D array and another one for freeing it. I still use them! You can see the code here if you like.

    And just think about functions you already use: imagine if you had to rewrite the entire code for printf() in main() every time you wanted to print some text on the screen.
    Source for the quote from an old thread of this forum.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i have no idea what's wrong
    By Adam Rinkleff in forum C Programming
    Replies: 4
    Last Post: 07-26-2011, 10:42 PM
  2. Replies: 4
    Last Post: 10-18-2006, 08:30 AM
  3. i have no idea what wrong
    By firefly in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2005, 12:42 AM
  4. Wrong Idea
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-21-2004, 11:52 AM