Functions
Functions are pre-written codes that perform a certain task. Depending on how it is written, whether it is part of a class and how we import it, we can call a function by simply typing its name or using dot notation. Some functions require us to pass data in for them to perform their tasks. These data are known as parameters and we pass them to the function by enclosing their values in parentheses separated by commas.
Example
To display text in a program, we use the print() function. We call it by typing print("Hello World") where print() is the function and "Hello World" is the parameter.
To use the replace() function for manipulating strings, we type "Hello World".replace("World", "Universe") where replace is the name of the function and "World" and "Universe" are the parameters. The string before the dot in the block of code will be affected with the function, changing it from "Hello World" to "Hello Universe".
No comments:
Post a Comment