You can call a function by using the following types of formal arguments − The function accomplishes nothing and finally this would produce the following result − Changing mylist within the function does not affect mylist. The parameter mylist is local to the function changeme. Print "Values outside the function: ", mylist Print "Values inside the function: ", mylist Mylist = # This would assig new reference in mylist "This changes a passed list into this function" Following is the example to call printme() function − Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. "This prints a passed string into this function"ĭefining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code. The following function takes a string as input parameter and prints it on standard screen. A return statement with no arguments is the same as return None.īy default, parameters have a positional behavior and you need to inform them in the same order that they were defined. The statement return exits a function, optionally passing back an expression to the caller. The code block within every function starts with a colon (:) and is indented. The first statement of a function can be an optional statement - the documentation string of the function or docstring. You can also define parameters inside these parentheses. Here are simple rules to define a function in Python.įunction blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).Īny input parameters or arguments should be placed within these parentheses. You can define functions to provide the required functionality. These functions are called user-defined functions. but you can also create your own functions. Functions provide better modularity for your application and a high degree of code reusing.Īs you already know, Python gives you many built-in functions like print(), etc. A function is a block of organized, reusable code that is used to perform a single, related action.
0 Comments
Leave a Reply. |