Is there a difference between a subroutine and a function in C++?
Printable View
Is there a difference between a subroutine and a function in C++?
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;
}
Can you call a subroutine?
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;
}
Thanks for that input Wynd. :)
Also, just a terminology thing: there are no subs in C++. Everything is called functions (or methods for classes). :)
In objective-C you call them messages :p
Anyways the procedures you call functions in a functional programming language and methods in a object oriented programming language :)