Results 1 to 7 of 7

Thread: subroutine and Functions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22

    subroutine and Functions

    Is there a difference between a subroutine and a function in C++?
    Eena

  2. #2
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Not really. A subroutine just doesn't return anything (void return type).
    Code:
    void mysub(int a) // Void function (sub)
    {
      cout<<a<<endl;
    }
    
    int myfunc(int a) // Function returns something
    {
      return a + 5;
    }
    Alcohol & calculus don't mix.
    Never drink & derive.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Can you call a subroutine?
    Eena

  4. #4
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Yeah, look at this example code.
    Code:
    #include <iostream>
    using namespace std;
    
    // Function prototype
    void mysub(int);
    int myfunc(int);
    
    int main()
    {
    	int a, b;
    
    	cout<<"Enter a number:"<<endl;
    	cin>>a;
    
    	mysub(a); // Prints the value of a
    
    	b = myfunc(a); // Value of b is now 5 more than the value of a
    
    	return 0;
    }
    
    // Actual function code
    void mysub(int a) // Void function (sub)
    {
    	cout<<a<<endl;
    }
    
    int myfunc(int a) // Function returns something
    {
    	return a + 5;
    }
    Alcohol & calculus don't mix.
    Never drink & derive.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Thanks for that input Wynd.
    Eena

  6. #6
    Also, just a terminology thing: there are no subs in C++. Everything is called functions (or methods for classes).

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    In objective-C you call them messages
    Anyways the procedures you call functions in a functional programming language and methods in a object oriented programming language
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

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



Click Here to Expand Forum to Full Width