Lab 10
Lab 10
LAB # 10
OBJECTIVE
Implement functions in Python and , also find that using functions makes your
programs easier to write, read, test, and fix. In this lab you’ll also learn ways to pass
information to functions.
THEORY
Functions:
Defining a Function:
You can define functions to provide the required functionality. Here are simple rules
to define a function in Python.
• Function blocks begin with the keyword def followed by the function name
and parentheses ( ( ) ).
• The code block within every function starts with a colon (:) and is indented.
Syntax:
def functionname( parameters ):
“””code in the function”””
1
Programming Fundamentals (CT-153) Practical Workbook
function_suite
return [expression](value to return to the main program)
Example#01:
✓ This example shows the simplest structure of a function. The line at 1 uses
the keyword def to inform Python that you’re defining a function. This is the
function definition, which tells Python the name of the function and, if
applicable, what kind of information the function needs to do its job. The
parentheses hold that information. In this case, the name of the function is
greet_user (), and it needs no information to do its job, so its parentheses are
empty. (Even so, the parentheses are required.) Finally, the definition ends in
a colon.
✓ Any indented lines that follow def greet_user (): make up the body of the
function. The text at line 2 is a comment called a docstring, which describes
what the function does. Docstrings are enclosed in triple quotes, which
Python looks for when it generates documentation for the functions in your
programs.
✓ The line print ("Hello!") is the only line of actual code in the body of this
function, so greet_user () has just one job: print ("Hello!"). When you want to
use this function, you call it. A function call tells Python to execute the code
in the function. To call a function, you write the name of the function,
followed by any necessary information in parentheses, as shown at x.
Because no information is needed here, calling our function is as simple as
entering greet_user (). As expected, it prints Hello!:
Output:
Output:
2
Programming Fundamentals (CT-153) Practical Workbook
Calling a Function:
Defining a function only gives it a name, specifies the parameters that are to be
included in the function and structures the blocks of code.
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. Following is the example
to call printme() function
Arguments:
An argument is a piece of information that is passed from a function call to a
function. When we call the function, we place the value we want the function to
work with in parentheses.
You can call a function by using the following types of formal arguments:
• Required arguments
• Keyword arguments
• Default arguments
Required Arguments:
Required arguments are the arguments passed to a function in correct positional
order. Here, the number of arguments in the function call should match exactly with
the function definition.
To call the function printme(), you definitely need to pass one argument, otherwise
it gives a syntax error as follows –
3
Programming Fundamentals (CT-153) Practical Workbook
Output:
Keyword Arguments:
Keyword arguments are related to the function calls. When you use keyword
arguments in a function call, the caller identifies the arguments by the parameter
name.
This allows you to skip arguments or place them out of order because the Python
interpreter is able to use the keywords provided to match the values with
parameters.
Name: miki
Age 50
4
Programming Fundamentals (CT-153) Practical Workbook
Default Arguments:
A default argument is an argument that assumes a default value if a value is not
provided in the function call for that argument. The following example gives an idea
on default arguments, it prints default age if it is not passed –
Name: miki
Age 50
Name: miki
Age 35
Arbitrary Arguments:
If you do not know how many arguments that will be passed into your function, add
a * before the parameter name in the function definition. This way the function will
receive a tuple of arguments, and can access the items accordingly:
def my_function(*kids):
print("The youngest child is " + kids[2])
5
Programming Fundamentals (CT-153) Practical Workbook
Lab Exercise: