Python Programming - Functions
Functions are a structured and reusable organization that can only be used to perform practical actions. Python Function applies regularly and high level of reuse.
As you have already learned, Python provides many native functions like print (), but you can also create your own work. These employees are called User Defined.
Defining a function
You can describe the jobs you need to pay for the work you need. Here are some general rules for describing Python operations.
The syntax is:
For example,
The next task is using the string as a wireless print on the screen.
The definition of work gives the name, defines the limits to be included in waste disposal and waste construction.
When the basic workflow is complete, you can delete it by calling another functionality or directly in the Python chart. Then, for example, you need to call the print action ()
All the boundaries of the Python language are referred to as references. If you change the boundaries of technology, change also returns to call functions. For example -
Function Arguments:
You can call the technique using the following types of formal arguments:
Required arguments:
Required arguments are arguments submitted to appropriate action in the correct position. Here, the number of calls per call must define the functionality.
To call the publish task (), you must log in properly, otherwise provide the following to the error device -
Keyword arguments:
The term discussion is based on call functionality. When you use key arguments in action, the caller describes the arguments using the control name.
This allows you to ignore the discussion or post the request because the Python interpreter can use displayed words to match the boundary value. You can make important calls about Print Jobs () in the following ways:
As you have already learned, Python provides many native functions like print (), but you can also create your own work. These employees are called User Defined.
Defining a function
You can describe the jobs you need to pay for the work you need. Here are some general rules for describing Python operations.
- Activity types start with the basic language, followed by the job name and cards ().
- All edges or panels should be placed inside. You can also drill holes in these articles.
- The first evidence of action is voluntary training - documented or documented work.
- The pulse code for each function starts with a lump (:) and goes back.
- The Return Statement excludes the [submission] action and voluntarily submits to the candidate. The only evidence to back up the argument is the same.
The syntax is:
Def function name (parameters):If they are not appreciated, the boundaries are of a temporary nature and need to be reported as described.
"Function_docstring"
function_suite
Return [expression]
For example,
The next task is using the string as a wireless print on the screen.
printme def (str):Calling the Function:
"This will print the string passed in this function"
Print str
Return
The definition of work gives the name, defines the limits to be included in waste disposal and waste construction.
#!/usr/bin/python # Function definition is here def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme("I'm first call to user defined function!") printme("Again second call to the same function")When the previous code was executed, it yielded the following results -
I'm first call to user defined function!
Again second call to the same function
Call by reference vs valueAll the boundaries of the Python language are referred to as references. If you change the boundaries of technology, change also returns to call functions. For example -
#!/usr/bin/python # Function definition is here def changeme( mylist ): "This changes a passed list into this function" mylist.append([1,2,3,4]); print "Values inside the function: ", mylist return # Now you can call changeme function mylist = [10,20,30]; changeme( mylist ); print "Values outside the function: ", mylistHere, we will continue to refer to previous points and add value to the same thing. After that, it yields the following results -
Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
Function Arguments:
You can call the technique using the following types of formal arguments:
- Required arguments
- Keyword arguments
- The default arguments
- Variable length arguments
Required arguments:
Required arguments are arguments submitted to appropriate action in the correct position. Here, the number of calls per call must define the functionality.
To call the publish task (), you must log in properly, otherwise provide the following to the error device -
#!/usr/bin/python # Function definition is here def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme()TypeError:
Traceback (most recent call last):
File "test.py", line 11, in <module>
printme();
TypeError: printme() takes exactly 1 argument (0 given)
Keyword arguments:
The term discussion is based on call functionality. When you use key arguments in action, the caller describes the arguments using the control name.
This allows you to ignore the discussion or post the request because the Python interpreter can use displayed words to match the boundary value. You can make important calls about Print Jobs () in the following ways:
#!/usr/bin/python # Function definition is here def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme( str = "My string")When the previous code is executed, it produces the following results -
My string
Comments
Post a Comment