|
-
Nov 10th, 2001, 06:38 PM
#1
Thread Starter
Junior Member
subroutine and Functions
Is there a difference between a subroutine and a function in C++?
Eena

-
Nov 10th, 2001, 07:00 PM
#2
Fanatic Member
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.
-
Nov 10th, 2001, 07:51 PM
#3
Thread Starter
Junior Member
Can you call a subroutine?
Eena

-
Nov 10th, 2001, 09:48 PM
#4
Fanatic Member
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.
-
Nov 10th, 2001, 11:24 PM
#5
Thread Starter
Junior Member
Thanks for that input Wynd.
Eena

-
Nov 11th, 2001, 10:43 AM
#6
Member
Also, just a terminology thing: there are no subs in C++. Everything is called functions (or methods for classes).
-
Nov 11th, 2001, 12:23 PM
#7
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|