Python Basics
Theory: Function parameters

Functions can also accept parameters as well as return parameters. In this lesson, we'll learn how to create these functions.
Remember that we've already encountered function parameters:
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.
Here is what using this function would look like:
We can draw the following conclusions from this example:
- We need to define the
get_last_char()function - The function must accept one parameter, which needs to be a string, as input
- The function must return a string
Defining the function:
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:
The value of the parameter will depend on how we call the function:
The parameter must be specified. If you call the function without it, the interpreter will give an error:
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:
The same works with methods. They can require any number of parameters to work:
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.
Below is an example of the definition of a function called replace() that replaces one part of a string with another:
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:
You now know how to create functions that can take parameters as input.
Completed
0 / 43