HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>Functions can also accept parameters as well as return parameters. In this lesson, we'll learn how to create these functions.</p>
1 <p>Functions can also accept parameters as well as return parameters. In this lesson, we'll learn how to create these functions.</p>
2 <p>Remember that we've already encountered function parameters:</p>
2 <p>Remember that we've already encountered function parameters:</p>
3 <p>Now imagine that we need to implement a function called get_last_char() that returns the last character in the string we passed to it as a parameter.</p>
3 <p>Now imagine that we need to implement a function called get_last_char() that returns the last character in the string we passed to it as a parameter.</p>
4 <p>Here is what using this function would look like:</p>
4 <p>Here is what using this function would look like:</p>
5 <p>We can draw the following conclusions from this example:</p>
5 <p>We can draw the following conclusions from this example:</p>
6 <ul><li>We need to define the get_last_char() function</li>
6 <ul><li>We need to define the get_last_char() function</li>
7 <li>The function must accept one parameter, which needs to be a string, as input</li>
7 <li>The function must accept one parameter, which needs to be a string, as input</li>
8 <li>The function must return a string</li>
8 <li>The function must return a string</li>
9 </ul><p>Defining the function:</p>
9 </ul><p>Defining the function:</p>
10 <p>The name of the text variable that serves as a parameter in parentheses. The parameter name can be anything. The main thing is that it reflects the meaning of the value it contains. For example:</p>
10 <p>The name of the text variable that serves as a parameter in parentheses. The parameter name can be anything. The main thing is that it reflects the meaning of the value it contains. For example:</p>
11 <p>The value of the parameter will depend on how we call the function:</p>
11 <p>The value of the parameter will depend on how we call the function:</p>
12 <p>The parameter must be specified. If you call the function without it, the interpreter will give an error:</p>
12 <p>The parameter must be specified. If you call the function without it, the interpreter will give an error:</p>
13 <p>Many functions work simultaneously with several parameters. For example, to round numbers, you need to specify not only the number itself but also the number of decimal places:</p>
13 <p>Many functions work simultaneously with several parameters. For example, to round numbers, you need to specify not only the number itself but also the number of decimal places:</p>
14 <p>The same works with methods. They can require any number of parameters to work:</p>
14 <p>The same works with methods. They can require any number of parameters to work:</p>
15 <p>To create such functions and methods, you need to specify the required number of parameters, separated by commas, in the definition. They also need to be given clear names.</p>
15 <p>To create such functions and methods, you need to specify the required number of parameters, separated by commas, in the definition. They also need to be given clear names.</p>
16 <p>Below is an example of the definition of a function called replace() that replaces one part of a string with another:</p>
16 <p>Below is an example of the definition of a function called replace() that replaces one part of a string with another:</p>
17 <p>When there are two or more parameters. The order in which we pass the parameters is important for almost all functions. If you change it, the function will work differently:</p>
17 <p>When there are two or more parameters. The order in which we pass the parameters is important for almost all functions. If you change it, the function will work differently:</p>
18 <p>You now know how to create functions that can take parameters as input.</p>
18 <p>You now know how to create functions that can take parameters as input.</p>