d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
Prev14567856Next
Add Reply New Topic New Poll
Member
Posts: 125
Joined: Jun 19 2012
Gold: 0.00
Jul 18 2012 12:32am
If you're talking about "flow", Code: The Hidden Language of Computer Hardware and Software is a good book.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 18 2012 01:55am
NOOB QUESTION: Can someone outline the differences between functions and objects?

IIRC Objects had like classes and inheritance or something to that extent, but it has been awhile since I touched the java tut.
Member
Posts: 279
Joined: Apr 11 2010
Gold: 1,486.00
Jul 18 2012 06:39am
Objects are things. A square is an object.

Functions are actions. Calculating the area of a square is a function.
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Jul 18 2012 12:19pm
Objects are a bit more abstract than functions. A function is exactly what it means, even in mathematical terms: for a given set of inputs, a function will give a certain output.

i.e. f(x) = 10x is a function
Code

def foo(x):
 return 10*x


As Pumbles said, in broader terms, functions are actions, much like verbs in a sentence.

Objects are a construct that we use to represent different *things* in a program, like we'd use nouns in a sentence. You may perform actions on them, and they may perform actions on themselves or on each other.

A class (of objects) is like a category of them. A class defines the shared traits of all the objects in this category. For example, an Integer might be considered a class, and you might have some Integer objects (aka "instances") such as 1, 2, 3, 4, 5, etc.

Typically, the most confusing thing about Objects and Classes is understanding the difference between them. The simplest analogy that you can understand them with is that a Class definition is sort of like a blueprint, while objects/instances are what get created from this blueprint. As in the example above, you can create as many different Integer objects as you like (1, 2, 3, 4, 1, 1, 1, 1), but the class of Integers itself is unique. There is exactly one kind (class) of animal called a giraffe, but there are many (instances of) giraffes in the world.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 18 2012 12:27pm
Quote (irimi @ Jul 18 2012 01:19pm)
Objects are a bit more abstract than functions.  A function is exactly what it means, even in mathematical terms: for a given set of inputs, a function will give a certain output.

i.e. f(x) = 10x  is a function
Code
def foo(x):
 return 10*x


As Pumbles said, in broader terms, functions are actions, much like verbs in a sentence.

Objects are a construct that we use to represent different *things* in a program, like we'd use nouns in a sentence.  You may perform actions on them, and they may perform actions on themselves or on each other.

A class (of objects) is like a category of them.  A class defines the shared traits of all the objects in this category.  For example, an Integer might be considered a class, and you might have some Integer objects (aka "instances") such as 1, 2, 3, 4, 5, etc.

Typically, the most confusing thing about Objects and Classes is understanding the difference between them.  The simplest analogy that you can understand them with is that a Class definition is sort of like a blueprint, while objects/instances are what get created from this blueprint.  As in the example above, you can create as many different Integer objects as you like (1, 2, 3, 4, 1, 1, 1, 1), but the class of Integers itself is unique.  There is exactly one kind (class) of animal called a giraffe, but there are many (instances of) giraffes in the world.


ahhh, thanks. Seems like it could be interesting to work with them at some point. I've heard you can do object oriented stuff in c++? If that is so then I might get to do some stuff in class in the fall semester.
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Jul 18 2012 01:13pm
C++ is an object-oriented language, so yes. It's not my favorite language to learn or teach OOP with, though. Java beats most languages for teaching and learning OOP, hands down.

This post was edited by irimi on Jul 18 2012 01:14pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 18 2012 02:39pm
I've only seen the basic stuff atm but am growing more fond of c++ as time goes on.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 21 2012 03:41am
Code
#include <iostream>
using namespace std;

const int N = 3;
double getdouble();
bool isinorder(double x, double y, double z);
void printresults(string x, double y);
void instructions(int n);
double total(double x, double y, double z);

int main()
{
   instructions(N);  //prompts the user for 3 pos numbers (doubles)
   double x=getdouble();
   double y=getdouble();
   double z=getdouble();

   if (isinorder(x,y,z)) cout <<"Theyre in order.\n";
   else cout <<"Theyre not in order.\n";

   double sum = total(x,y,z);
   double avg = sum/N;

   printresults("total: ",sum);
   printresults("avg: ",avg);


   return 0;
}

void instructions(int n)
{
   if (n == 1) cout<<"Please type in a positive double";
   else cout<<"Please type in "<<n<<" positive doubles";
   cout <<".\n";
}

bool isinorder(double x, double y, double z)
{
   if (x <= y && y <= z) return true;
   return false;
}

double getdouble()
{
   double d;
   do
   {
       cin >> d;
       if (d < 0) cout << "Please enter a positive number." << endl;
   }
   while (d < 0);
   return d;
}

double total(double x, double y, double z)
{
   return x + y + z;
}

void printresults(string x, double y)
{
   cout << x << y << endl;
}


Okay so this assignment took a whole 5 minutes to do. Prof. gave us a template he had written (you can tell some parts were done by him and not me), basically to fill in the blank with a bunch of functions.

I have tried to change the way I indent things so let me know how messy it still is. REALLY want to get this down tight. Things like else/if situations w/o blocks of code, spaces between lines of code, etc. I would like to know anything helpful in this area.

If you think there are any improvements I could make lmk as well. The 10000 functions are there because as I said, he was testing our ability to create functions/pass args/etc.

Another thing that confused me was the function for instructions. Like he set that whole thing up. Apparently he wasn't aware that you cannot decrement a constant? (hence constant...)

I just left it as is. Might email him about it...

edit: I was originally thinking of decrementing some variable 'n' in that function every ime a new double was entered, but.....I didn't think it was necessary.

This post was edited by Eep on Jul 21 2012 03:47am
Member
Posts: 279
Joined: Apr 11 2010
Gold: 1,486.00
Jul 21 2012 09:54am
The only "bug" I see is that you are counting 0 as a positive number.

As far as style, there are four things I'd call out. First, I always use brackets in if/else statements, even if the block is only one line. It makes all programs look the same and makes it easier to add more to the block later. Second, whenever you have an expression like

Code

if (whatever) {
   return true;
}
else {
   return false;
}


you can always just shorten that to return (whatever);. If you'd return false in the if case and true in the else case, it can be return !(whatever);. Third, I recommend camel case for function names (isInOrder, printResults, etc) since it makes them easier to read. Fourth, I recommend spaces between function arguments (inorder(x, y, z) vs inorder(x,y,z)) again because it makes the program easier to read.

It sounds like the last two might be decisions the professor has made for you. More important than any style preference is working in the same style as the rest of your team.
Member
Posts: 21,154
Joined: Apr 2 2006
Gold: 4,501.00
Jul 21 2012 12:39pm
hey, i program on c, python, you could make an infinte loop when you ask for information, and ONLY IF the information introduced was right, then you proceed to close the loop and then the program keeps going

an example in c

....

int inf=0, num1;

while (inf==0)
{
printf("\nIntroduce me one number between 1-1000");
scanf("%i", &num1);

if (num1>1 && num1<1000)
{
inf=1;
}
}
Go Back To Programming & Development Topic List
Prev14567856Next
Add Reply New Topic New Poll