Robotics & Artificial Intelligence
What are the various parameter passing techniques?
Python Functions
1 Like
Answer
Python supports the following parameter passing techniques:
- Fixed Length Arguments: These are the arguments defined in the function header. While calling the function, the values must be passed in the same order and number as specified in the function definition.
- Variable Length Arguments: Python allows a function to accept a variable number of arguments. There are two types:
*args: Stores the passed arguments in a tuple.**kwargs: Stores the passed arguments in a dictionary.
- Default Arguments: These arguments have a default value assigned in the function definition. If no value is provided during the function call, the default value is used. Default arguments must appear after non-default arguments.
Answered By
3 Likes
Related Questions
Write a function for checking the speed of drivers. This function should have one parameter: speed.
- If speed is less than 70, it should print "OK".
- Otherwise, for every 5 km above the speed limit (70), it should give the driver one demerit point. Calculate and print the total number of demerit points at the end. For example, if the speed is 80, it should print: "Demerit Points: 2".
- If the driver gets more than 12 demerit points, the function should print: "License suspended".
Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter). For example, if the limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
Describe user-defined functions in Python with the help of an example.
Distinguish between void functions and non-void functions with the help of an example.