Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Functions in Python: Library Functions, User-Defined Functions, and Parameters, Summaries of Computer science

Programming LanguagesData StructuresAlgorithmsSoftware Engineering

The concept of functions in Python, focusing on library functions, user-defined functions, and their parameters. It covers the syntax for defining and calling functions, as well as the difference between formal and actual parameters. Additionally, it discusses the concept of default parameters and the scope and lifetime of variables.

What you will learn

  • What is the difference between formal and actual parameters?
  • How do you define a user-defined function in Python?
  • What are the three types of functions in Python?

Typology: Summaries

2020/2021

Uploaded on 09/09/2021

srinivas-3
srinivas-3 🇮🇳

1 document

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download Functions in Python: Library Functions, User-Defined Functions, and Parameters and more Summaries Computer science in PDF only on Docsity! CHAPTER-2 FUNCTIONS IN PYTHON Definition: Functions are the subprograms that perform specific task. Functions are the small modules. Types of Functions: There are three types of functions in python: Library Functions: These functions are already built in the python library. Functions defined in modules: These functions defined in particular modules. When you want to use these functions in program, you have to import the corresponding module of that function. User Defined Functions: The functions those are defined by the user are called user defined functions. Library Functions in Python: These functions are already built in the library of python. For example: type(), len(), input() ete. Functions defined in modules: Functions of math module: To work with the functions of math module, we must import math module in program. import math iinport math S.No. | Function Description Example 1 sqrt() Retumas the square root of a number >>>maih sqrl(49) 10 2 ceil() Returns the upper integer >>>math.cell(8L3) 82 3 floor() Returns the lower integer >>>math floor(81.3) 8L 4 pow) Calculate the power of a number >oomath.pow(2,3) 8.0 5 febs() Returns the absolute valve of a number >SSanath fabs(-5.6) 56 6 exp() Returns the e raised to the power Lee >>>math exp) 20.085536923187668 Function in random module: randint( )- function generates the random integer values including start and end values. Syntax: randint(start, end) It has two parameters. Both parameters must have integer values. Example: import random n=random.randint(3,7) *The value of n will be 3 to 7. User defined functions: The syntax to define a function is: def function-name ( parameters) : #statement(s) Keyword def marks the start of function header. A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in Python. Parameters (arguments) through which we pass values to a function. They are optional. A colon (:) to mark the end of function header. One or more valid python statements that make up the function body. Statements must have same indentation level. print(s1, s2, s3) OUTPUT: 7711 b. Function not returning any value : The function that performs some operations but does not return any value, called void function. def message(): print("Hello") m=message() print(m) OUTPUT: Hello None Scope and Lifetime of variables: Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope. There are two types of scope for variables: i) Local Scope ii) Global Scope Local Scope: Variable used inside the function. It cannot be accessed outside the function. In this scope, the lifetime of variables inside a function is as long as the function executes. They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls. Global Scope: Variable can be accessed outside the function. In this scope, Lifetime of a variable is the period throughout which the variable exits in the memory. Example: def my_func(): x = 10 print("Value inside function:",x) x= 20 my_func() print("Value outside function:",x) OUTPUT: Value inside function: 10 Value outside function: 20 Here, we can see that the value of x is 20 initially. Even though the function my_func()changed the value of x to 10, it did not affect the value outside the function. This is because the variable x inside the function is different (local to the function) from the one outside. Although they have same names, they are two different variables with different scope. On the other hand, variables outside of the function are visible from inside. They have a global scope. We can read these values from inside the function but cannot change (write) them. In order to modify the value of variables outside the function, they must be declared as global variables using the keyword global. Very Short Answer Type Questions (1-Mark) Q1. What is default parameter? Ans: A parameter having default value in the function header is known as a default parameter. Q2. Can a function return multiple values in python? Ans: YES. Short Answer Type Questions (2-Marks) Q1. Rewrite the correct code after removing the errors: - def SI(p,t=2,r): return (p*r*t)/100 Q2. Consider the following function headers. Identify the correct statement: - 1) def correct(a=1,b=2,c): 2) def correct(a=1,b,c=3): 3) def correct(a=1,b=2,c=3): A) def correct(a=1,b,c): Q3.What will be the output of the following code? a=1 def f0: a=10 print(a)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved