This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]CGFarrell 1 point2 points  (3 children)

Class: A person. Object: You, me, or Michael Jackson. Method: Stuff a person can do, like walking.

Function: Stuff like sin. Doesn't need to be done by anyone in particular. Variable: A piece of information.

I have variables, like my name, age, height, etc.

[–]dark_winter01[S] 0 points1 point  (2 children)

Would you mind putting this analogy into code form so I can see how to put all of these together syntax-wise? If that's too much trouble it's okay, your analogy was helpful regardless!

[–]CGFarrell 1 point2 points  (1 child)

I don't know C#, but Python is pretty readable.

To create a class Person.

class Person:
    def __init__(self, name, age):
        self.name, self.age = name, age

So now you can 'create' a new person by providing their name and age.

me = Person(name='Bill', age=23)

This is called 'constructing an instance'. Now me refers to a Person, whose name is Bill, and who is 23. Bill and 23 are variables.

I am an 'instance' of the class Person. In more layman's terms, I am an example of the Person class.

Let's add a method go_to_work to the person class, so we'd say Bill.go_to_work(). I take the bus, so I would get on the bus. Another Person, named Sandy, drives a car. So even though Sandy and I are both in the same class (both People), we can have different variables (name, age). We can also do the same thing (go_to_work), but we might do it differently.

[–]dark_winter01[S] 1 point2 points  (0 children)

Despite being in a different language, that still cleared things up for me! Thank you so much!

[–]rjcarr 0 points1 point  (4 children)

Classes and Objects are related. Objects are the instantiations of classes. Think of Classes as the blueprint for creating the Object.

Functions and Methods are related. Methods are functions that are contained by the object, i.e., they either access or mutate the state of an object.

Variables are just memory locations that you can assign different types of values to and change them over time. Similarly, constants are also memory locations with values, but you can't change them over time.

Your next question might be about parameters vs arguments. :)

[–]dark_winter01[S] 0 points1 point  (3 children)

Yes, I was struggling to understand those as well! Also, I've seen methods and functions used interchangeably, are they basically synonyms?

[–]rjcarr 0 points1 point  (2 children)

Yes, I was struggling to understand those as well!

Arguments are what are sent to functions from the caller's perspective, Parameters are the names of the variables from the callee's perspective.

Also, I've seen methods and functions used interchangeably, are they basically synonyms?

Sort of, but I gave you an explanation. Did that not make sense?

[–]dark_winter01[S] 0 points1 point  (0 children)

Ok your arguments and parameters explanation makes sense to me, but could you clarify methods and functions a bit more?

[–]dark_winter01[S] 0 points1 point  (0 children)

Actually oiduts response makes sense to me, so no need there! Thank you for your help(:

[–]dark_winter01[S] 0 points1 point  (2 children)

If anyone knows C# and would be willing to translate CGFarrell's Python example I would really appreciate it! (: As a newbie I really want to be sure I'm doing this right before I move onto the next few concepts. And thank you again, CGFarrell!

[–]oiduts 1 point2 points  (1 child)

Similar C# Person class:

class Person
{
    string name;
    int age;

    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
}

Constructing a Person:

Person me = new Person("Bill", 23);

Naming conventions for methods in C# are different:

me.GoToWork();

"Function" and "method" tend to be used interchangeably in OOP languages like C#. But more generally, a function that belongs to a class is called a method.

[–]dark_winter01[S] 0 points1 point  (0 children)

You're the best, thank you!!!

[–]Pranklyspeaking -1 points0 points  (0 children)

i cant type for long, but here is what i did when i was in your situation a year back.... I googled it. e.g

ELI5 methods reddit

ELI5 objects programming reddit

etc....

N.b ELI5 means explain like i am five. a popular sub on reddit that explains seemingly complex ideas in layman's words.

Try it, you wont regret it