0% found this document useful (0 votes)
15 views5 pages

Grade 12 - Functions Computer Notes

Uploaded by

YOGA SAI GUHAN
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)
15 views5 pages

Grade 12 - Functions Computer Notes

Uploaded by

YOGA SAI GUHAN
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
Key Points Introduction: P A funetion is a collection of statements which is made to perform a specifie task. Instead of writing a large program, we can write small fimetions as a specilic part of program to accomplish the tas! To Execute function, we have to call it in the program, Once written, a function can also be used in other programs as library functions. a v DAY tA ad olas te ee } Ginette These are pre — defined functions and are always available for use. In order to use these functions, there is no need to import any module. We can directly use it, in our program, . Examples : ons defined in Modules 7 These functions are pre modules. + These can only be used when the correspondi module is imported, defined in particular Question bank - Class Xit (Computer Sci By Swati Chawla 30 statistics module User — Defi ‘These functions are defined by the programmer. > Functions that we define ourselves to do certais specific task are ferred as _user-deli fictions. fact() ‘ax of User ~ Defined Functions {et Functionnam ( {Parameters} }: | | ] Statements | [cstatements3] J" to be | executed Keyword Name of Function (must follow the ules of Identifier) Keyword def marks the beginning of the function header. Function Name must follow the naming rules same as for identifiers. Function may or may not contain argument: ‘A colon (:) marks the end of function header Function can take one or more statements to perform a specific task. Anoptional return statement to return a value from the function. > Function must be called / invoked to execute its code. } All statements within the same block must have same indentation. Examples of User Defined Function: def Display (): print( my) \ vvy v Display () Never Give Up!!! Output phew ast Aad) = nunisint (input (0 er nun2=int (input ("~ aber "D)] Variations in User Defined Functions «Functions with no argument and no return value. © Functions with arguments but no return value. © Functions with arguments and return value. © Functions with no argument but return value. > The return statement ends the function execution even if it is in the middle of the function. The statements written afier return statement wi || not get executed. > Ifa function does not return any value, by default it returns None. > A function can return more than one value in the form of tuple. Question bank - Class xil (Computer Science) 31 5 y Swati Chawla \iof Operations (a,b) lentor firet number : 25 lenter second number : 5 125, 5.0) Zelass_‘tuple'>] IResuit = (30, 20, [type of Result = > The multiple return value must be either stored in TUPLE or we can UNPACK the received value by specifying same number of variables: on the Ieft of function call Arguments and Parameters: CBSE Sample Paper (2020-21) Parameters Values being received are called parameters. Parameters appears in Function Header oF ‘Arguments Values being passed are called arguments. ‘Arguments appear in the function call statement, Function Definition. ‘Arguments are also | Parameters are also known as Actual | known as Formal Arguments or Actual | Parameters or Formal Parameter Argument jaf Add (ni -n2): —> result-nl¢n2 xeturn result al and n2 are Formal Arguments wumi=int (input ( » yum2=int (input ( ” Add (nun nun2)—> uml and num2 are int ( y=) Actual Arguments red Arguments : Positional arguments are arguments passed to a function in correct positional order. Ifwe change theit order, then the result will be changed. If we change the number ofarguments passed, then it shall result in an error. When the fimetions call statement must match the number and order of arguments as defined in the function definition this is called positional argument matching. 2. Default Arguments : Default argument is an argument that assumes a default value if'a value is not provided in the function call for that argument, If you are not passing any values will be considered. Non default arguments arg In a function header, any parameter cannot have a default value unless all parameters appearing on its right have their default values, ‘luc, then only default cannot follow default ents. wit consider default value lortie3 ens sae result-Sinpleinta print Principle = Rate = 5 [Eime = 3 DefeuleVatve ts taken, ‘Simple Interest = 750.0 5000 3. Keyword / Named Arguments : The default argument gives flexibility to specify default value for a parameter so that it can be skipped in the function call, ifneeded, However, still we can not change the order of arguments in function call. To get control and flexibility over the values sent as arguments, python offers Keyword Arguments. This allows to call function with arguments in any order using name of the arguments. Rules for combining all three types of Arguments > An argument list must first contain positional arguments followed by any keyword arguments, > Keyword arguments should be taken from the required arguments preferable, > You cannot specify a value for an argument more than once. Flow of Execution The flow of execution refi 8 to the order in which Question bank - Class XII (Computer Science) By Swati Chavela 32 Tact calesuntx,y): Jp aexty returns |» punt=int (input |onum2—int (input ( Sun-calesun (num ,mun2) bpeine ft def increment (): Ip x=x+1 [3 4iimain program [Ider power(b,p): z=b*#p return © calcsquare (a) : a=power (a,2) return a 10 result=calcsquare (n) DREEREAGnaen Scope of a variable is the portion of a program whe the variable is recognized. OR SCOPE means part of'a program in which a variabl can be used, Scop. The variables declared inside the function definitior are called Local Variables lef funel () : ; < Mere, Lisa Local Variable. print ( £ 2) ‘unc () @ |Inside funcl.. 20) uy Lofat variable not cfssble outside th fonction. Tile se-\Uaera\ael\appbata\ toca] esther Global Scope : The variables declared outside the function definition are called Global Variables. J——> Here, Gis a Gacal Variable. 1s) loutside Function : 20 [G=20} def func (): print ("inst (Global Variable is accessible inside as well as loatside the Funct race wats (outside Function What if, a same variable is declared inside as well as outside the function? Preference will be given to Local Variable, inside the function, (output [inside Function = 50 Joutside Function : 20) So, if both can have save variable names, How can a function change the value of a global variable in Local Scope? Using global keyword Question bank ~ Class Xtl (Computer Science) 33 By Swati Chawla la lef func(): global ® 50 print (ioeid> Jtunct Iprint ("9 nek Inside Function : 50 Output) ieside Function + 59 > If in any program, there are two variables with same name one is local and one is global, to use the global variable in a function we have to use the global keyword with variable name. > When a function changes the value of a global variable, it will be reflected back outside the function also. Difference between Local and Global Vai [ Global Variable Tt is a variable which is declared outside all the functions. ble Local Variable It is a variable which is declared within a function or within a block. It is accessible only| It is accessible within a function / block | throughout the program. . in which it is declared. | LEGB Rule E Gososis) 6 Gam B Gita» Mutable_And_Imm Objects Depending upon the mutability or immutability of the data type, a variable behaves differently. Immutable Type : Ifa variable is referring to an Immutable type then, any change in its value will also change the memory address it is referring to. Any change in the value of a immutable type will not get reflected in the original value outside the funetion. % Mutable Type : If a variable is referring to mutable type, and then any change in the value of mutable type will not change the memory address of the variable, it will change the value in place. Any change in the value of Mutable type wi bie_Properties_ OF Data reflect in the original value outside the function also. If a parameter is assigned a new name or different data type value, then changes will not reflected back in original value. Scan this QR Code For Video Explanation of Functionsin Python

You might also like