13 continue the methods in C #, with the return value method introduced

Source: Internet
Author: User
Tags integer division

In this exercise, we're going to use a method with a return value. If a method takes a return value, it is in the form of this.

Defines a C # method with a return value

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

{

Your code

return value

}

Static: What do you mean by not having a tube? Before you say what it means, add it in every method you write.

Return type: The data type we talked about earlier, int,string, and so on. Usually a method call is given to return a value where this method is called.

Method Name: You can give the method a name you want. Can't afford to be blind. Naming rules are the same as naming rules for variables. Have you forgotten? Let's take a look at the front stuff.

Parameters: Without parameters, the parentheses inside will not write anything, need a few parameters, write a few parameters, the parameters are separated by commas. What is the parameter type? It seems that your perception is not enough. is the data type. What is the name of the parameter? Oh my God! Is the same as the variable name.

Return statement: Returns the value you need to return

How do I call a method that has a return value?

If the method has a return value, you want to save the return value. Then just define a variable, the type of the variable needs to be the same as the type of the method's return value. Use this variable to connect to the return value of the method. form as follows.

numeric type variable name;

Variable name = Method Name (variable 1 or a data value, variable 2 or a data value); Of course the parameter can be either one or no, there can be more than one, see what you need.

Let's take a look at the actual code, and more specifically the use of methods with return values in C #.

In this exercise, we created a new method in the class program. This method is used to calculate the title of a second grade in primary school. Some friends go out to play, to taxi, each taxi up to four people, ask at least a few taxi? In the main method is responsible for the user to enter a total number of people, and then call this method, this method calculates the number of at least a taxi, return to the Main method. The main method outputs the result.

You create a project. The solution name is SLOUTION13, the project name is Exercise13, and the project type is a console program. Click Program.cs. Add the code to the inside.

There are several points of knowledge in this code that need to be explained.

1. Line 17th. people = Int. Parse (Peopleinput); Where Peopleinput is used to connect users through Console.ReadLine (); However, this value is string-based. We need to turn it into int type. Here I have introduced a new method.

Int. Parse (argument of string type), whose return value is an integer. If a string type argument cannot be converted to an integer, the program will error. If the string is "15", it can be converted to an integer of 15. If the string is "tinking Jiang", it will be an error.

2. Line 28th. Takes the remainder of the integer division. The maths in the second grade of primary school often has such a problem. For example 7/4 = 1 of the remainder is 3. The 7%4=3.

Run results

Practice

1. Read the program, annotate each line of code, and explain how they work

2. Design a math problem and write a method to calculate it.

3. On the Internet, is there any other way to calculate the number of taxis required? Try to rewrite one's own method.

  

13 continue the methods in C #, with the return value method introduced

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.