1

I'm a beginner/intermediate programmer going from Java to C# and has been doing a little reading on functions on codeproject.com. My understanding is that namespace in C# is equivalent to packages in java. Using a namespace is the same as importing a package, though it's possible to have nested namespace. Classes exist within namespaces, and methods exist within classes. But I read somewhere that functions are class independent? Are functions not methods then? Does that mean that in C#, you can define functions outside of classes within namespaces, and can use them like methods in a class once you import a namespace? In practical usage, say if I'm trying to write a Math Library, would it be better to write functions or static methods? Are there any difference in terms of usage between the two?

As you can tell, I'm also a little mixed-up on the actual definition of a function. Is it a property (mathematical property where each input has single output) that any code can have? If yes, does that mean static methods are also functions (as long as it doesn't depend on a variable outside its scope)? Is it a special type of method/routine defined with delegate/other syntax that can be passed as parameters and assigned to variables? Responses to the question on stack overflow difference between methods and functions seems to differ quite a bit, and the accepted answer is different from the top results from googling.

I realize there are multiple questions, but they are seem interrelated (at least to me). All these definitions are bouncing around my poor head, and anything that clarifies the concept or correct my misconception would be a great help. Explanations with examples would be amazing, since I'm having trouble with the terminology. Thank you.

Community
  • 1
  • 1
Ellyl
  • 61
  • 4
  • 1
    This depends upon the definitions used. In general a *method* is a *type of function* that is bound to an instance. So methods *are* functions, with an additional property/restriction. However, other sorts of *"functions"* (ie. Func, Action, delegate in C#) are "independent" of any instance. Properties are generally not considered functions at all (although some property may represent/evaluate-to functions, and 'get'-properties in C# can execute arbitrary code..). – user2864740 May 17 '15 at 23:10
  • @user2864740 Gods, functions are confusing. So function here is different from the mathematical definition of function then (the part about one input one output)? By independent, do you mean that they can be used outside of a class, or that they must be declared in a class but can be accessed without instantiating the class? – Ellyl May 17 '15 at 23:56
  • Only functional programming languages usually stick to a more strict definition, Haskell being one of the better examples. In other languages a function is generally "some 'predefined code' that is called to do stuff which might return a value and/or perform other side-effects". Not so tidy :) – user2864740 May 18 '15 at 00:01
  • Ah, the difference in definitions makes a lot of sense. Functions in general is different from functions in functional programming, the latter which corrospond with the mathematical definition. delegate, Func etc. are functions in both a functional programming and general sense, while methods are functions in the more general sense only. – Ellyl May 18 '15 at 00:24

1 Answers1

0

Let's keep it simple. No matter what the definition of functions, in C# or Java you have:

  • A package or a namespace which can only contain classes; not functions, nor methods.
  • Within a Class you have methods which would serve as functions, in the sense that they receive parameters and may output results.
  • You can have static methods, either in Java or C#. These methods are Class dependent, not object dependent, meaning they are called directly from a class without an object instance.
  • There are also entities called lambda expressions. These are objects which are defined as functions. These are used in functional programming.

And that's pretty much it. There are no functions defined directly from packages or namespaces, as there are no packages within packages nor namespaces within namespaces.

For a Math library, you may define a public Math class with all its functions, or in our case, static methods. Thus you would call: Math.sin(..), Mas.cos(...), and so on.

Note that lambda expressions serve a purpose of making some areas of programming easier, such as: SQL expressions in code, filtering collections, and so on.

Finally note that lambda expressions should not be confused with methods which belong to a class. I would suggest a nice reading from MSDN called Lambda Expressions. This reading would ease the understanding of such a nice, but dense topic.

EduardoFernandes
  • 3,091
  • 1
  • 13
  • 12
  • Got it. The part about class independence must be from C. Which of the many definitions do you refer to when you say "methods which would serve as functions by your definition"? Not all methods fulfills the mathematical definition of a function, since they may depend on global variables, which may be different. In the tutorial, functions are marked with delegate. Are they just a special type of method then? Are methods marked static and delegate both functions? – Ellyl May 17 '15 at 23:49
  • I corrected the part where I said "by your definition".... that's simply not fair to put it in that way. In the codeproject tutorial mentioned in the question, they are talking about lambda functions, which as a matter of fact, are objects which indeed are functions. – EduardoFernandes May 18 '15 at 00:30
  • I believe you confusion is justified because you picked up such a dense article to read. I would suggest fro you to start from something lighter, like, the article I mentioned in the response: https://msdn.microsoft.com/en-us/library/vstudio/bb397687%28v=vs.110%29.aspx – EduardoFernandes May 18 '15 at 00:36