Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
10 views
Working With Functions
python functions
Uploaded by
vijay
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save 3. Working with functions For Later
Download
Save
Save 3. Working with functions For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
10 views
Working With Functions
python functions
Uploaded by
vijay
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save 3. Working with functions For Later
Carousel Previous
Carousel Next
Save
Save 3. Working with functions For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 34
Search
Fullscreen
Unit i: Computational Thinking and Programming -2 Visit to website: learnpythondcbse.com Chapter-3: FUNCTIONS 1 Python: Functions 2 | Types of Functions 3 1. Functions with no parameters and no return values. 4 2. Functions with parameters and no return value. 5 3. Functions with parameters and return value. 6 4. Functions with Multiple Return Values 7 Function Parameters and Arguments 8 Python: Types of Argument 9 1. Positional Arguments: 10 | 2. Default Arguments 11 | 3. Keyword Arguments 12 | 4, Variable length Positional Arguments 13 _| 5. Variable length Keyword Arguments 14 | Functions and Scopes 1. Global Scope: 2. Local scope: 3. Nonlocal variable 15 | Mutable/immutable Properties of Data Objects 16 _ | Passing String to Functions Passing Lists to Functions 17 _| Passing Tuples to Functions Passing Dictionaries to Functions 18 | Functions Using Mathematical Libraries: 19 | String methods & built in function: Page of 34Unit i: Computational Thinking and Programming -2 ‘Visit to website: learnpythondcbse.com Chapter-3: FUN Co}. E) * A function is like a subprogram A small program inside of a program * The basic idea: — We write a sequence of statements — And give that sequence a name — We can then execute this sequence at any time by referring to the sequence’s name * Functions reduce code duplication and make programs more easy to understand and maintain * Having identical (or similar) code in more than one place has various downsides: — Don’t want to write the same code twice (or more) — The code must be maintained in multiple places — Code is harder to understand with big blocks of repeated code everywhere * Functions are used when you have a block of code that you want to be able to: — Write only once and be able to use again * Example: getting input from the user — Call multiple times at different places Page 2 of 34Unit i: Computational Thinking and Programming -2 ‘Visit to website: learnpythondcbse.com Chapter-3: FUNCTIONS + Example: printing out a menu of choices — Differ a little bit when you call it each time + Example: printing out a greeting to different people * Code sharing becomes possible Keyword def 5 fans Function Body * Italways starts with the keyword def (for define) «next after def goes the name of the function (the rules for naming functions are exactly the same as for naming variables) * after the function name, there's a place for a pair of parentheses (they contain nothing here, but that will change soon) «the line has to be ended with a colon; * the line directly after def begins the function body - a couple (at least one) of necessarily nested instructions, which will be executed every time the function is invoked; note: the function ends where the nesting ends, so you have to be careful. * Function must be called/ invoked to execute. Page 3 of 34Visit to websit (Computation: mapythonacbse.com Chapter-3: FUNCTIONS We're ready to define our first program using function, = The function is extremely simple, but fully usable. We've named it communication, inking and Pro let’s create it. def communication (): print ("Welcome to python programming’s World. ") print ("We start here.") print ("We end here.") Note: we don't use the function at all in the above program - there's no invocation or calling of it inside the code. When you run the above code, you see the following output: We start here. We end here. This means that Python reads the function's definitions and remembers them, but won't launch any of them without your permission. Now we've modified the above code We've inserted the function's invocation between the start and end messages: Invocation/Calling of functi def communication( ): World.") peint("Welcome to python programming? print("We start here.") --» communication() @- print ("We end here.") Page dof 34‘omputational Thinking and Programming -2 ‘Visit to website: learnpythondcbse.com Chapter-3: FUNCTIONS Uni The output looks different now: We start here. Welcome to python programming’s World. We end here. It tries to show you the whole process: © when you invoke a function, Python remembers the place where it happened and jumps into the invoked function; + the body of the function is then executed; ‘* reaching the end of the function forces Python to return to the place directly after the point of invocation 1. Functions with no parameters and noreturn values. * Apython function without any arguments means you cannot pass data * A function which does not return any value cannot be used in an expression it can be used only as independent statement. * Let's have an example to illustrate this. function_prog.py - DyAmjad_CS/function_prog py (3.6.5) ull uur gett jee File Edit Format Run Options Window Help # 1. Functions with no arguments and no return values. oan Semearten be inction without parameters print ("Welcome to python programming’s World. " print ("We start here.") = communication () print ("We end here.") No return keyword is used ion body Ld Cok Page of 34Unit i: Computational Thinking and Programming -2 Visit to website: learnpythondcbse.com Chapter-3: FUNCTIONS OUTPUT: We start here. Welcome to python programming’s World. We end here. Before discussing second form of python’s function we must know, * A parameter is a variable that is initialized when we call a function + parameters exist only inside functions in which they have been defined * Parameters are given in the parenthesis ( ) separated by comma. * Values are passed for the parameter at the time of function calling. How Parameters Work? * Each function is its own little subprogram > Variables used inside of a function are local to that function > Even if they have the same name as variables that appear outside that function * The only way for a function to see a variable from another function is for that variable to be passed in as a parameter We can create a communication () function that takes in a person's name (a string) as a parameter * This type of function can accept data from calling function * We can control the output of function by providing various values as arguments. Page 6 of 34Unit i: Computational Thinking and Programming -2 Visit to website: learnpythondcbse.com Chapter-3: FUN Co}. E) * Let's have an example to get it better. [reser Bite cencion peer 865) Zz [Ftc tat Format Ran Options Window Help #2. Functions with parameters and no return value. seats [Parameter ‘name’ declared in function def communication (name): 4 print ("Welcome to",name, "in python programming’s World. ") ! print ("We start here.") ‘communication ("Amit") |_ Calling of a function with argument ‘Amit’ print ("We end here.") tnis Coro OUTPUT: We start here. Welcome to Amit in python programming’s World. We end here. tees ead Parameters are the name within Arguments are the values passed in the function definition. when the function is called. Parameters don’t change when the | Arguments are probably going to be program is running different every time the function is called. This is also known as formal Arguments are also known as actual parameters parameters. _—— oo [—— Function Parameter or Formal parameter def communication (name) print ("Welcome to",name, "in python programming’s World. " Iprint ("We start here.") Function Argument or Actual Parameter of communication ("Amit") ———+ | 3 ginction print ("we end here. Page 7 of 34Unit i: Computational Thinking and Programming -2 ‘Visit to website: learnpythondcbse.com Chapter-3: FUN Co}. E) 3. Functions with parameters and return value. — To have a function return a value after itis called, we need to use the return keyword Handling Return Values * When Python encounters return, it > Exits the function > Returns control back to where the function was called > Similar to reaching the end of a function The value provided in the return statement is sent back to the caller as an expression result The return value must be used at the calling place by - > Either store it any variable > Use with print() > Use in any expression * Let's have an example to illustrate this. La function prog py - DYAmjad_CS/unction prog py (3.65) File Edit Format Run Options Window Help #3. Functions with parameters and return value. def Area (side): R = side*side return R def main(): $ = float(input ("Enter the side of square:") ar = Area(s) print ("The area of square is: ",ar) main() Page of 34
You might also like
Working With Functions
PDF
100% (1)
Working With Functions
34 pages
Functions
PDF
No ratings yet
Functions
34 pages
CH-3 Notes and Questions-1
PDF
No ratings yet
CH-3 Notes and Questions-1
28 pages
Functions
PDF
No ratings yet
Functions
31 pages
Unit III Python
PDF
No ratings yet
Unit III Python
20 pages
Python - 1 Year - Unit-4
PDF
No ratings yet
Python - 1 Year - Unit-4
120 pages
Lecture 3 Functions PartI Sep09 2018
PDF
No ratings yet
Lecture 3 Functions PartI Sep09 2018
28 pages
Python Notes
PDF
No ratings yet
Python Notes
73 pages
Functions
PDF
No ratings yet
Functions
41 pages
Code Reusability
PDF
No ratings yet
Code Reusability
33 pages
Python Unit III
PDF
No ratings yet
Python Unit III
8 pages
Unit 4
PDF
No ratings yet
Unit 4
70 pages
Lecture 7 Functions PartI
PDF
No ratings yet
Lecture 7 Functions PartI
34 pages
Logic & Algorithm: Lec02a - Function in Python
PDF
No ratings yet
Logic & Algorithm: Lec02a - Function in Python
42 pages
Python - 1 Year - Unit-4
PDF
No ratings yet
Python - 1 Year - Unit-4
114 pages
User Defined Functions in Python Chapter 2 (1)
PDF
No ratings yet
User Defined Functions in Python Chapter 2 (1)
16 pages
CH 3 Function
PDF
No ratings yet
CH 3 Function
31 pages
Functions in Python
PDF
No ratings yet
Functions in Python
22 pages
Python Programming Unit-IV
PDF
No ratings yet
Python Programming Unit-IV
28 pages
12th Cs ch-3 Working With Functions
PDF
No ratings yet
12th Cs ch-3 Working With Functions
112 pages
Chapter 2
PDF
No ratings yet
Chapter 2
27 pages
Python Unit 2
PDF
No ratings yet
Python Unit 2
14 pages
PPS Unit 3
PDF
No ratings yet
PPS Unit 3
44 pages
Lecture 5
PDF
No ratings yet
Lecture 5
45 pages
Functions Notes 1
PDF
No ratings yet
Functions Notes 1
24 pages
MODULE 2
PDF
No ratings yet
MODULE 2
17 pages
Unit 2 User Defined Functions Stds
PDF
No ratings yet
Unit 2 User Defined Functions Stds
9 pages
Python C C Presentation 2
PDF
No ratings yet
Python C C Presentation 2
73 pages
Python Functions
PDF
No ratings yet
Python Functions
25 pages
Functions Python
PDF
100% (1)
Functions Python
39 pages
Function, String
PDF
No ratings yet
Function, String
16 pages
10.python Functions
PDF
No ratings yet
10.python Functions
27 pages
Working With Functions
PDF
No ratings yet
Working With Functions
33 pages
L1 2 3unit3
PDF
No ratings yet
L1 2 3unit3
56 pages
PPS Unit 3
PDF
No ratings yet
PPS Unit 3
50 pages
Unit - 1 Functions
PDF
No ratings yet
Unit - 1 Functions
11 pages
Python Functions
PDF
No ratings yet
Python Functions
26 pages
Unit 3
PDF
No ratings yet
Unit 3
31 pages
Chapter 5 part 1 pfe
PDF
No ratings yet
Chapter 5 part 1 pfe
30 pages
Unit III Python
PDF
No ratings yet
Unit III Python
12 pages
ECONTENT OF FUNCTIONS CHAPTER OF PYTHON XII
PDF
No ratings yet
ECONTENT OF FUNCTIONS CHAPTER OF PYTHON XII
18 pages
Y Math - SQRT (144.0)
PDF
No ratings yet
Y Math - SQRT (144.0)
20 pages
Class 12 CS F 1
PDF
No ratings yet
Class 12 CS F 1
6 pages
Python Unit3
PDF
No ratings yet
Python Unit3
130 pages
Learn Python 3 - Functions Reference Guide - Codecademy
PDF
No ratings yet
Learn Python 3 - Functions Reference Guide - Codecademy
7 pages
Functions
PDF
No ratings yet
Functions
21 pages
Chapter 4.functions
PDF
No ratings yet
Chapter 4.functions
54 pages
Chapter-2 Function
PDF
100% (1)
Chapter-2 Function
40 pages
Working With: Chapter - 3
PDF
No ratings yet
Working With: Chapter - 3
22 pages
PPS Unit - 3
PDF
No ratings yet
PPS Unit - 3
47 pages
Class12_CS_Chapter7
PDF
No ratings yet
Class12_CS_Chapter7
36 pages
Python Functions 1
PDF
100% (1)
Python Functions 1
49 pages
Unit 1 (Practical A-E)
PDF
No ratings yet
Unit 1 (Practical A-E)
50 pages
Pps Unit-3
PDF
No ratings yet
Pps Unit-3
10 pages
Py4Inf 04 Functions
PDF
No ratings yet
Py4Inf 04 Functions
23 pages
UNIT-II
PDF
No ratings yet
UNIT-II
82 pages
Unit III
PDF
No ratings yet
Unit III
196 pages
ch 3
PDF
No ratings yet
ch 3
26 pages
DOC-20250328-WA0019.
PDF
No ratings yet
DOC-20250328-WA0019.
16 pages
Class 12 - Evaluation of Postfix ( Stack )
PDF
No ratings yet
Class 12 - Evaluation of Postfix ( Stack )
12 pages
1 Mark Questions DBMS
PDF
No ratings yet
1 Mark Questions DBMS
92 pages
Create Table Student
PDF
No ratings yet
Create Table Student
2 pages
Input in Python
PDF
No ratings yet
Input in Python
2 pages
Drone Technology
PDF
No ratings yet
Drone Technology
14 pages
CBSE Class 12 Computer Science Compartment Answer Key
PDF
No ratings yet
CBSE Class 12 Computer Science Compartment Answer Key
24 pages
Preamble
PDF
No ratings yet
Preamble
1 page
Muthayammal Engineering College, Rasipuram: Department of Computer Science and Engineering
PDF
No ratings yet
Muthayammal Engineering College, Rasipuram: Department of Computer Science and Engineering
9 pages