To call a function means that you are telling the program to execute the function. How to call a function Call a function with a return value In case you do not define a return value, the function will return None. In the example above, the function returns result. You can define what to return by the return keyword. Docstrings are enclosed by triple quotes '''you will write the docstring here'''įunction returns: Python functions returns a value. You can pass as many parameters as needed to a function.įunction docstrings: These are optional constructs that provide a convenient way for associated documentation to the corresponding function. In the example above, the parameters are num1and num2. In Python, parameters are enclosed in parentheses. The parameters help to generalise the transformation/computation/task that is needed to be done. Parameter list: Parameter list are place holders that define the parameters that go into the function. In the example above, add_two_numbers is the parameter name. PEP8, which is a style guide for Python, recommends that in case multiple words are used, they should be in lowercase and they should be separated with an underscore. The function name comes after the def keyword. Keyword def: This is the keyword used to say that a function will be defined now, and the next word that is there, is the function name.įunction name: This is the name that is used to identify the function. You can look at the example below where a function returns the sum of two numbers. Note that the function block is indented by 4 spaces Please note that the body of the function is indented by 4 spaces. In Python to create a function, you need to write it in the following manner. Functions are the simplest, and, sometimes the most useful, tool for writing modular code. Similar code can be easily grouped into functions and you can provide a name to the function that describes what the function is for. Functions are useful because they provide a high degree of modularity. A function is a block of code that takes in some data and, either performs some kind of transformation and returns the transformed data, or performs some task on the data, or both.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |