Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

pps notes 1st year

Rating
-
Sold
-
Pages
12
Uploaded on
20-06-2024
Written in
2023/2024

PPS handwritten notes are additional remarks made after the main text, often written by hand to provide extra details, explanations, or updates. They serve to enrich the content by addressing related points or emphasizing key information that may not have been fully covered in the original document. These notes can clarify complex concepts, offer new insights, or respond to questions raised during the discussion. PPS notes are valuable for ensuring comprehensive understanding and are commonly used in academic papers, business communications, and personal correspondence to supplement and enhance the main message.

Show more Read less
Institution
Course

Content preview

Unit 03 Functions and Modules (Q & A for End Sem Examination)

Q1. What is function? Why we need functions, Explain its types.
Answer-
 Function Definition-
A function is a block of organized and reusable program code that performs single, specific, and well defined task.
It is piece of code that perform a well defined task.
 Need of Function-
1. Dividing the program into separate well defined functions facilitates each function to be written and tested separately.
2. User-defined functions are reusable code blocks; they only need to be written once, then they can be used multiple times.
3. Functions are very useful, writing common utilities to specific business logic.
4. These functions can also be modified as per requirement.
5. The code is usually well organized, easy to maintain, and developer-friendly. Which means it can support the modular
design approach.
6. As user-defined functions can be written independently, the tasks of a project can be distributed for rapid application
development.
7. A well-defined and thoughtfully written user-defined function can ease the application development process.
 Types of Functions-
1. Built-in functions- These are the predefined function in python.
e.g. min(), max(), print(), input()
2. User-Defined Functions (UDFs)- These are user or programmer created functions.
3. Anonymous functions/lambda function- The function which is created without def keyword. These function does not
have any name , mostly have single line code
--------------------------------------------------------------------------------------------------
Q2. What is user defined function(UDF)? How we define and call user define function. Give suitable example.?
Answer-
 User Defined Functions-
1. Besides using built-in functions, users can also write their own functions. Such functions are called user defined functions.
2. User-defined functions are reusable code blocks; they only need to be written once, then they can be used multiple times.
 Defining a Function-
Defining a function means specifying its name, parameters that are expected and set of instructions.
There are four steps to defining a function in Python-
Step 1: Declare the function with the keyword def followed by the function name.
Step 2: Write the parameters inside the opening and closing parentheses of the function, and end the declaration with a colon.
Step 3: Add the program statements to be executed.
Step 4: End the function with/without return statement.
 Syntax to create user defined function
def functionname (para1, para2, para3 ...): # function header
program statement1
program statement3 #function body
program statement3
....
return expression

 Function Call-
1. Function is called using function name and specifying number of argument in parentheses
2. When function is called, the interpreter checks that the correct number and type of arguments are used in the function call.
3. List of variables used in function call is known as the actual parameter list.
4. Actual parameters may be variables, expressions or constants.

 Syntax to Call/Execute user defined function-

functionname(arg1,arg2...)


Example of User defined function-




PPS UNIT 3 Functions and Modules By- Er. P. N. Shingote

, Example of User defined function-

#Defining function
def my_function( ):
print("Hello from a function")

#calling function
my_function()

Output- Hello from a function
------------------------------------------------------------------------------------------------
Q3. What is use of return statement in function? give syntax and example.
Answer-
 return statement-
1. The return statement exit a function.
2. return statement passing back an expression to the calling function
3. The return statement with no argument is same as return None.
4. If function don’t have return statement, then by default it returns None.
5. Function can return multiple values using return statement.
6. Multiple values from function retrun as tuple, so these multiple values are assigned to multiple variables based on
sequence.
 Syntax for return statement-

return [expression]

 Example of return statement-returning single value
#Program to add two numbers
def add(a,b): #function definition
c=a+b
return c #return value of c

sum=add(10,20) #function call
print(sum)

In above example, def is keyword, a and b are the parameters , c=a+b is the statement/expression executed by function.
return statement return value of c to the caller. We can call above function as - sum=add(10,20)
 OUTPUT-
30
 Example of retrun statement return multiple values
#Defining function
def sumprod(a,b):
c=a+b
d=a*b
return c,d #return statement, returns multiple values c and d

# function call
s,p=sumprod(10,20) #returned values c and d are stored in variable c and d respectively
print(“s= “,s) #30
print(“p= “,p #200


Q4. Explain function parameters and arguments.what are types of arguments? Give suitable examples.
Answer-
 Function parameters and arguments-
1. Parameter is a variable in a function definition.
2. Argument is the actual value of the variable that gets passed to function.
3. When a function is called, the arguments are the data you pass into the function’s parameters.




PPS UNIT 3 Functions and Modules By- Er. P. N. Shingote

Written for

Institution
Course

Document information

Uploaded on
June 20, 2024
Number of pages
12
Written in
2023/2024
Type
Class notes
Professor(s)
Sid gunjal
Contains
All classes

Subjects

$4.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sidgunjal

Get to know the seller

Seller avatar
sidgunjal avcoe
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions