The document discusses Python functions including what they are, how to define them, passing arguments, arbitrary arguments, keyword arguments, default parameters, and returning values. Functions are blocks of code that can be executed and reused by calling them. Arguments allow passing data into functions.
The document discusses Python functions including what they are, how to define them, passing arguments, arbitrary arguments, keyword arguments, default parameters, and returning values. Functions are blocks of code that can be executed and reused by calling them. Arguments allow passing data into functions.
Chapter 6: Python Functions What are Python Functions?
● Python Functions are blocks of code that runs only when
they are specifically called in the program. ● They can be executed repeatedly or as an when they are required by the program. ● We can pass data inside them in the form of arguments or parameters. ● We can call the function using its name. ■ Example: myFunction() Example of a Python Function
● A Function can be created in Python using the def keyword.
● Example of a Python Function with zero arguments. What are Function Arguments?
● Function Arguments or Parameters are the information
that we pass to the function. ● The term Arguments or Parameters are used interchangeably and mean the same thing that is information passed to the function. ● A function can take single or multiple arguments. Example: Function with Single Argument ● We need to call the function with its proper parameters for the function to execute successfully. ● display() has 1 parameter which is fname, so we need to pass fname a value, when calling the function display(). Example: Function with Multiple Arguments ● display() has 4 arguments and we have to pass 4 arguments to it for the function to execute. Function with unequal number of Arguments
● A Function if defined with two arguments, will
compulsorily require two arguments to be executed. ● If a Function is called with unequal number of arguments it will throw an error. ● The below error message says, display() function is missing 1 argument. Example: Error when Arguments are not equal Using Arbitrary Arguments
● If we don't know how many arguments our function will
need then we can use Arbitrary Arguments. ● Arbitrary Argument is declared with a * symbol. ● The function will receive a tuple of data. ● We can then loop through the tuple to retrieve the required data. Example of Function with Arbitrary Args Using Keyword Arguments
● To implement keyword arguments, we need to declare
arguments as key value pairs. ● Value of the keys will be passed to the function when calling the function. ● We can then print the value of the key inside the function by calling print() and passing the function arguments to it. Example of Function with Keyword Arguments Using Arbitrary Keyword Arguments
● If we are not sure about how many keyword arguments our
function needs, we can create a function with a parameter having double asterisk(**). ● This will create a dictionary of arguments. ● We can then print the values using the keys of the dictionary. Example of Function with Arbitrary Keyword Arguments Using Default Parameter Value
● We can declare a default value to the parameter of the
function. ● So if the function is called without any arguments or parameters it will still execute. ● If we want to change the default parameter value then we can pass the new value as argument to the function call. Example of Default Parameter Value Using Return with Python Functions
● To return anything from the function, we can use return
keyword. ● Any value or variable that we return will be returned from the function. ● Such value or variable can then be stored in another variable for further processing. Example of Returning Data from a function Thank You