Thread: Does C Support Subroutines

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    41

    Does C Support Subroutines

    Does C support the use of anything similar to a subroutine? I have code that I would like to include logic in such a manor that it will execute and then return to the next line of code after executing the subroutine, but may want to run the same code in several places without having to key in the same code multiple times. Is this possible?

  2. #2
    Indeed C and C++ support subroutines. For futher information, view this link.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Does a bear #!*% in the woods?

    C is built on functions which are just subroutines on steroids.
    Code:
    #include <stdio.h>
    
    void print_a_message(char *msg)
    {
      printf("%s\n", msg);
    }
    
    int main(void)
    {
      printf("Here comes the message!\n");
      print_a_message("first message");
      printf("Here comes another message!\n");
      print_a_message("another message");
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    int foo(int value)
    {
         return value *2;
    }
    int main(int argc, char *argv[])
    {
        int i=0;
        for(i=1;i<5;i++)
        {
              printf ("%d\n", foo( i ) );
         }    
    }
    Output:
    2
    4
    6
    8

    foo() is repeatedly called to do the same job.

    C supports only functions - like foo() and main() above. No subroutines.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Yes they are called functions if I am getting what you are saying they look like this
    Code:
    int add(int var1, int var2)
    {
       
       return (var1 + var2);
    
    }
    
    int main(void)
    {
    
       int a = 3;
       int b = 5;
       int total = add(a,b);
       /*total will equal a + b which is 8 */
       printf("Total equals %i", total);
      /*think this is how printf works :) */
      total = add(total,a);
      /*total now equals total + a which is 11 */
      printf("Total equals %i", total);
      return 0;   
    
    }
    Woop?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >C supports only functions - like foo() and main() above. No subroutines.
    Technically, a function is a subroutine that returns a value. So C does support subroutines in the technical sense because functions are subroutines. In the conventional sense, subroutines are functions that don't return a value, so C supports subroutines in the conventional sense because functions can be written to return void. Though in the C world, there's no distinction and both are called functions.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    When coming from a procedural language like BASIC or something similar, it is hard to get used to the modularity of C/C++. It takes some time but it offers a lot of advantages for programming real-world applications, and can save you time if you use the same code a lot.

    Using functions is what is used 99.99% of the time. There does exist the ability to use labels
    Code:
    label:
    and goto
    Code:
    goto label;
    just like in the aforementioned procedural languages. I should however warn, that this will result in flaming on this board. It is commonly looked upon as "bad style". There's nothing inherently wrong with it, it's just that most of the time functions will do a much better job, and there's no reason not to use functions.

    I have code that I would like to include logic in such a manor that it will execute and then return to the next line of code after executing the subroutine, but may want to run the same code in several places without having to key in the same code multiple times. Is this possible?
    I'm pretty sure you're looking for functions, but you may also find loops useful. If you want to perform the same piece of code many times on each of a set of objects (for example, elements in an array), you can use loops. Since this is probably not what you're looking for right now, I won't go into an explanation, but I recommend you look at a tutorial on loops next time you're looking to learn something new, if you haven't learned them already.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Prelude
    In the conventional sense, subroutines are functions that don't return a value, so C supports subroutines in the conventional sense because functions can be written to return void.
    Just remember that main is never a subroutine! (At least in the conventional sense.)
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  4. I hate tech support....
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-20-2001, 08:59 AM