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

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.

Typology: Summaries

2020/2021

Uploaded on 09/09/2021

srinivas-3
srinivas-3 🇮🇳

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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( ) etc.
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
pf3
pf4
pf5

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( ) etc. 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

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.

Syntax: function-name(parameter) Example: ADD(10,20) OUTPUT: Sum = 30. The return statement: The return statement is used to exit a function and go back to the place from where it was called. There are two types of functions according to return statement: a. Function returning some value b. Function not returning any value a. Function returning some value : Syntax: return expression/value Example-1: Function returning one value def my_function(x): return 5 * x Example-2 Function returning multiple values: def sum(a,b,c): return a+5, b+4, c+ S=sum(2,3,4) # S will store the returned values as a tuple print(S) OUTPUT: (7, 7, 11) Example-3: Storing the returned values separately: def sum(a,b,c): return a+5, b+4, c+ s1, s2, s3=sum(2, 3, 4) # storing the values separately

print(s1, s2, s3) OUTPUT: 7 7 11 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.

return (prt)/ 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):
  4. def correct(a=1,b,c): Q3.What will be the output of the following code? a= def f(): a= print(a)