12 methods in C,

Source: Internet
Author: User

12 methods in C,

Remember our first program? Forgot? You have to work hard. Our first program is to output some strings to the dos window. In which program there is only one method, the Main method. The Main method is a special method, but it is also a method. Why is the Main method Special? You can check it online. If you cannot find it, remember to ask me.

C # programs are composed of many classes. Each class contains some methods. We can leave the class empty. In our previous exercises, we all met Program. cs. Program is a class. For the moment, we understand the class as a container. We can put methods in the class container. The subsequent exercises will put all the methods in the Program class.

 

Define a method

 

Static return type method name (name of parameter type parameter 1, name of parameter type parameter 2)

 

{

Your code

 

Return Value

}

 

Static: Do not worry about what it means. Before talking about its meaning, you must add it to every method you write.

 

Return type: the data type, int, string, and so on. Generally, after a method is called, a value is returned for the place where the method is called. What if my method does not want to return anything? No way. Ah? Joke with you. If your method has nothing to return, the return type is void.

 

Method Name: You can give the method a name you want. Cannot be blind. The naming rules are the same as those for variables. Forgot? Let's take out the previous items.

 

Parameters: If there are no parameters, nothing is written in the brackets. A few parameters are required, and the parameters are separated by commas. What is the parameter type? It seems that your understanding is not enough. Is the data type. How can I name a parameter? My God! Same as the variable name.

 

Why should we introduce the method?

 

In the real world, program functions are complex. A program includes dozens or even tens of thousands of features. If we put all these functions in one place, such as our Main method, this method will be too complicated. I don't know about Lego toys. You have never played it. A small plug-in can be used to assemble large buildings, ships, and even monsters. In modern programming ideas, it is also very similar to Lego toys. Break down a complex program into many simple units. The combination of these simple units can form many different programs. Many common units can be reused, greatly improving the development speed. These units can be understood as methods here. In addition, developing a program is usually completed by a team of several people. After dividing a program into different methods, each person can focus on his/her own responsibilities. After everyone completes the work, assemble it together.

 

How to call a method?

 

Add the following statement where you want to call it.

Method Name (variable 1 or a data value, variable 2 or a data value); Where variable 1 or a data value corresponds to the first parameter of the method, variable 2 or a data value corresponds to the second parameter of the method. The type of the variable or data value must be the same as the parameter type in the method definition.

If this method has a return value, you want to save the return value. You only need to define a variable. The type of this variable must be the same as the type of the return value of the method. This variable is used to pick up the return value of the function. Format.

 

Value type variable name;

Variable name = method name (variable 1 or a data value, variable 2 or a data value );

 

Next, let's take a look at the actual code and take a closer look at the c # method.

In this exercise, we created three new methods in the class Program. The first method carries a parameter. The second method has two parameters. The third method does not contain parameters. The function of the three methods is very simple, that is, to output some characters. If there is a parameter, output the parameter value. Then we call these three methods in sequence in the Main method.

You create a project. The solution name is Sloution12, the project name is Exercise12, and the project type is console program. Click Program. cs. Add the code to it.

 

 

Running result

 

Exercise

1. Create a new method. Then call it in the Main method.

2. Try to give this method some strange names. Look at the names that are not allowed by C.

3. Call a method with parameters. Can I leave it blank?

4. Can I pass a parameter to a method without parameters?

5. Create a new method and output something. Then modify the PrintWithOneParameter method and call the newly created method. Run the program and check the result.

 

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.