0% found this document useful (0 votes)
2 views18 pages

Lecture#10(Python)

This document provides an overview of functions in Python, detailing how to define and call them, as well as the types of function arguments including default, keyword, positional, and arbitrary arguments. It also introduces anonymous (lambda) functions and recursive functions, emphasizing the importance of base cases in recursion. The lecture is presented by instructor Rida Fatima.

Uploaded by

rida.maryam1215
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Lecture#10(Python)

This document provides an overview of functions in Python, detailing how to define and call them, as well as the types of function arguments including default, keyword, positional, and arbitrary arguments. It also introduces anonymous (lambda) functions and recursive functions, emphasizing the importance of base cases in recursion. The lecture is presented by instructor Rida Fatima.

Uploaded by

rida.maryam1215
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Functions in Python

Lecture-10

Instructor Name: Rida Fatima


.

FUNCTIONS:
• Python Functions are a block of statements that does a
specific task.
• The idea is to put some commonly or repeatedly done task
together and make a function so that instead of writing the
same code again and again.
.
.

Defining-a-function:
• We can define a function, using def keyword. A function might take
input in the form of parameters. The syntax to declare a function is:
.

Here, we define a function using def that prints a welcome message when called.
.

Calling a Function
• After creating a function, call it by using the name of the functions
followed by parenthesis containing parameters of that particular
function.
.

Function Arguments
• Arguments are the values passed inside the parenthesis of the function.
A function can have any number of arguments separated by a comma.
.

• def function_name(parameters): Declares a function with optional


input parameters.
• Docstring ("""..."""): (Optional) Used to describe what the function
does.
• return expression: (Optional) Returns a value to the caller. If omitted,
the function returns None.
.

Types of Function Arguments


• Python supports various types of arguments that can be passed at the
time of the function call. Below are types of function argument types:.

1. Default Arguments
2. Keyword Arguments
3. Positional Arguments
4. Arbitrary Arguments
.

1. Default Arguments:
• A default argument is a parameter that assumes a default value if a
value is not provided in the function call for that argument.
.

2. Keyword Arguments:
• Values are passed by explicitly specifying the parameter names, so the
order doesn’t matter.
.

3. Positional Arguments:
• Values are assigned to parameters based on their order in the function
call.
.

4. Arbitrary Arguments:
• Arbitrary Arguments: allow a function to accept a variable number of
inputs. This is done using two special symbols:

• *args: collects extra positional (non-keyword) arguments as a tuple.


• **kwargs: collects extra keyword arguments as a dictionary.
.
.

Anonymous(lambda) Functions
• An anonymous function means that a function is without a name. As we
know, def keyword is used to define the normal functions and lambda
keyword is used to create anonymous functions.
.

Recursive Functions
• A recursive function is a function that calls itself to solve a problem. It is
commonly used in mathematical and divide-and-conquer problems.
Always include a base case to avoid infinite recursion.
THANK YOU

You might also like