C # Learning Experiences (function methods ),

Source: Internet
Author: User

C # Learning Experiences (function methods ),

Functions or methods are all written under the class. We can call them through the function name. First, we need to figure out why there are functions or methods? Here, we use the same way of thinking as the previous article, starting with its purpose or practical value. Sometimes we encounter this situation when writing a program. For example, calculate the factorial of a certain number. We will write a series of statements to get our results. However, if we need another factorial, We have to rewrite it again. Of course, if you say copy and paste, it is really good. But what if there are a lot of changes in some situations? This is obviously not a simple approach. Because of this, our functions or methods are available.

How can we define a method? First, we can say that the method is written below the class. Second, just like declaring a variable, you must first write a keyword or modifier. Then, it is the name of this method, which is the way we call it. Add the brackets and the method body in the curly brackets. The method body is a series of repeated statements, and the brackets are a format. If you ask what is written in brackets, the official saying is "parameter ". Why? For example, you wrote a method to calculate the mean. You wrote the method name and brackets and found that they were incorrect. Why? Because you didn't ask for the average of those numbers. Therefore, when defining a method, you need to write the data types in parentheses and separate them with commas. Then, when you reference it, write the numbers in parentheses. In this way, you can write different numbers to get different results, which is like a function in mathematics.

In fact, the struct or enumeration type is also a meaning. They are all created for certain needs, and they are not abrupt things. I suggest you find the simplest example and try to understand it. This is definitely better than rote memorization. To cope with more abstract concepts, we recommend that you use hierarchies. There is always a logical association between concepts. If you can understand their subordination, it is excellent. I really can't understand it. Just look for a few instances and it won't work if I want to backend the estimation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.