13. Continue with the method in C # and introduce the method with the returned value,

Source: Internet
Author: User
Tags integer division

13. Continue with the method in C # and introduce the method with the returned value,

In this exercise, we need to use methods with return values. If a method has a return value, the format is as follows.

 

Define a C # method with a returned value

 

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.

 

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.

 

Return Statement: return the value you want to return

 

How to call a method with a returned value?

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. Use this variable to obtain the return value of the method. Format.

 

Value 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 or multiple, it depends on your needs.

 

Next, let's take a look at the actual code and take a closer look at 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 a second-year primary school question. A few friends went out to play and needed to take a taxi. Each taxi allowed a maximum of four people. How many taxis should they take? In the main method, the user is responsible for entering the total number of people and then calling this method. This method calculates the minimum number of taxis and returns them to the main method. The main method outputs this result.

 

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

 

There are several knowledge points in this code, which need to be explained.

1. 17th rows. People = int. Parse (leleinput); in this example, peopleInput is used to receive the value passed in through Console. ReadLine. However, this value is string type. We need to convert it to the int type. Here I introduce a new method.

Int. Parse (a string type parameter). Its return value is an integer. If the string type parameter cannot be converted to an integer, the program reports an error. If the string is "15", it can be converted to an integer of 15. If the string is "tinking jiang", an error is returned.

2. 28th rows. Returns the remainder of an integer division. Mathematics in the second year of primary school often has such questions. For example, if the remainder of 7/4 = 1 is 3, then 7% 4 = 3.

 

Running result

 

 

Exercise

1. Read the program, add comments to each line of code, and explain their functions

2. Design a mathematical question and compile a method for calculation.

3. Check on the Internet. Is there any other way to calculate the number of taxis required? Try to rewrite your own method.

  

 

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.