+ 2
What is the difference between a function, and a class?
I am new in objectum oriented programming (C#), and not really see the difference between functions, and classes.
3 Réponses
+ 4
you could see a function as a class method.
classes also may have attributes
classes may have instances
classes provide rhe OOP approach( inheritance, interfaces, encapsulation....)
But in a certain way you are right:
the procedural aporoach can do it too... but less comfortable.
+ 3
a function is a group of onstructions that performs a specific task. like adding two numbers or finding which number is the greater one.
a class is something like a custom made variable. the standard variables are integer, real, string etc. but with class you can create your own variables. these in turn can have their own functions and make use of the already existing standard variables.
+ 2
Functions are like machine. You put something into these machines and then machine give you something. For example:
factorial(5);
//You get 120 from this factorial machine.
But when we look the classes, they can have this machines and also variables.
class A{
int factorial(int n)
{
if(n==1)
return 1;
else
return n*factorial(n-1);
}
int root(int n)
{
return sqrt(n);
}
}
//As you can see we have a class and inside of class A we have 2 machines that are called as factorial and sqrt.