0% found this document useful (0 votes)
10 views

Working With Functions

python functions

Uploaded by

vijay
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
0% found this document useful (0 votes)
10 views

Working With Functions

python functions

Uploaded by

vijay
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
You are on page 1/ 34
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 34 Unit 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 34 Unit 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 34 Visit 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 34 Unit 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 34 Unit 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 34 Unit 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