0% found this document useful (0 votes)
16 views112 pages

12th Cs ch-3 Working With Functions

Uploaded by

pratyushgullu
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)
16 views112 pages

12th Cs ch-3 Working With Functions

Uploaded by

pratyushgullu
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
You are on page 1/ 112

Unit-1

Computational Thinking and Programming-2

CH-3 WORKING WITH FUNCTIONS


Ch-3 Working with Functions

1) Introduction
2) Understanding Functions
3) Defining Functions in Python
4) Flow of execution in a Function call
5) Passing Parameters
6) Returning value from Functions
7) Composition
8) Scope of variables
9) Mutable/immutable properties of Passed data objects
Function is a collection of statements which is made to perform a specific task.
To Execute function we have to call it in the program.
Instead of writing a large program we can write small functions as a specific part of
program to accomplish the task.
Once written a function can also be used in other programs as library functions
• Functions can be categorized in three types
1. Built-in
2. Modules
3. User Defined
Built-in Functions
• These are the functions which are predefined in python we have to just call them to use.
• Functions make any programming language efficient and provide a structure to language.
• Python has many built-in functions which makes programming easy, fast and efficient.
• They always reside in standard library and we need not to import any module to use them.
Python Modules
• Module is a .py file which contains the definitions of functions and variables.
• Module is a simple python file.
• When we divide a program into modules then each module contains functions and
variables. And each functions is made for a special task.
• Once written code in modules can be used in other programs.
• When we make such functions which may be used in other programs also then we
write them in module.
• We can import those module in any program and we can use the functions.
help Function
• If you forgot that how a library function works then help( ) is very useful for this situation.
• If this function is used with any module then it provides the information and details of the
given module and its all the contents.
string Module
• We have already studied about string module in class XI. Here are
some other functions of string module
random Module
• When we require such numbers which are not known earlier e.g. captcha or any type of serial
numbers then in this situation we need random numbers. And here random module helps us.
This contains following functions-
User-defined Functions
• These are the functions which are made by user as per the requirement of
the user.
• Function is a kind of collection of statements which are written for a specific
task.
• We can use them in any part of our program by calling them.
• def keyword is used to make user defined functions.
Points to Remember…….
Points to Remember…….
Points to Remember…….
RULES FOR FUNCTION

1. DEFINE FUNCTIONS LIKE: len() | sum() |max() | min() | str()…

2. CREATE FUNCTION NAME LIKE A VARIABLE OR IDENTIFIER : FUN() | SUM() |CAL()

3. BODY OF A FUNCTION CALL ACCORDING TO FUNCTION CALL: loop | if else |input |output

4. IN CALLING NEED TO PASS VALUE OR IDENTIFIER: FUN(3,4) |FUN(A,B)

5. FUNCTION CAN RETURN ONE OR MORE THEN ONE VALUE AT A TIME, MULTIPLE

VALUES ARE CONVERTED AS TUPLE AUTOMATICALLY

6. FUNCTION NAME SHOULD BE SAME IN DEFINITION OR IN CALLING


Returning Values from Functions
Functions in python may or may not return a value.
There are two types of Functions:
1) Functions returning some value (non void functions)
2) Functions not returning any value (void functions)
Functions not returning any value (void functions)
The functions that perform some action or do some work
but do not return any computed any value or final value to
the caller are called void functions.
A void function may or may not have a return statement.
User defined functions with argument with return values
User defined functions with no argument with return values
Functions returning some value (non void functions)
The function that return some computed result in terms of a
value, fall in this category.
The computed value is returned using return statement.
Functions returning some value (non void functions)

The value being returned can be one of the following:


ØLiterals
ØVariables
ØExpressions
Parameters in python have to be some names i.e variables to
hold incoming values.
Argument as variables
Argument as literal
both literal argument One literal and one variable argument
Arguments as expression
One literal and one expression argument One variable and one expression argument
Parameters and Arguments in Functions
• When we write header of any function then the one or more values given to its
parenthesis ( ) are known as parameter.
• These are the values which are used by the function for any specific task.

• While argument is the value passed at the time of calling a function.


• In other words the arguments are used to invoke a function.
• Formal parameters are said to be parameters and actual parameters are said to be
arguments.
Passing Parameters/Arguments
Not valid because not
matched with parameters
Positional Arguments
Positional Arguments
Default Arguments
To be continue in next presentation…..
Flow of Execution
The flow of execution in Python refers to the order in which the
instructions in a Python program are executed. When a Python
program is executed, it is first parsed by the Python interpreter.
same
Mutable and Immutable properties of Data Objects
Mutable
Immutable
Different data type
Different data type

You might also like