|
-
Nov 9th, 2004, 04:55 AM
#1
Thread Starter
New Member
Help In Understanding Functions
Ok I Recently got into my book and i am up to functions and how they work and i am a bit confused maybe someone can explain better then my book is or use a better example.....
Code:
//sqrt.cpp -- use a square root function
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double cover; //use double for real numbers
cout << "How many square feet of sheets do you have?" << endl;
cin >> cover;
double side; //create another variable
side = sqrt(cover); // call function, assign return value
cout << "You can cover a square side of " << side;
cout << " feet\nwith your sheets.\n";
cin.get();
cin.get();
return 0;
}
im not grasping how this works exactly......
-
Nov 9th, 2004, 08:01 AM
#2
Do you know how mathematical functions work? In programming functions work roughly in the same way.
-
Nov 10th, 2004, 06:45 PM
#3
PowerPoster
A function merely does something in a seperate part of code.
For example,
Code:
int add2(int num){
return num + 2;
}
int main(){
int amount=0; //decalre original amount var
int newAmount=0; //declare new amount var
newAmount=add2(amount);
}
So the function add2 simply adds 2 to the amount that was entered into the function and returns it to the variable you asign to it.
So amount was equal to 0, it was then input into the add2 function which made it equal to 2. Now newAmount = 2.
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

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
|