Functions - in Python

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Calling Function: Examples

Examples of Functions being called: def function2( x ): return 2*x *b = function2 ( 4 ) print(b) b = 8* c = function2( 5 ) print ( c ) c = 10

What is a Function

*Could simply be a* *collection of CODE.* When we define a function we can call it whatever we want. *Remember to have proper indentation* *Ex:* def function1 ( ) : print ( "ahhh" ) print ( "ahhhhh 2" ) print ( "this is outside the function" )

Calling a Function

*You can simply type the name of the function in the line of code or you can assign it to a variable and print the variable.* ex: Function1( ) - and the function will print or we could assign it to a variable like so: a = function1( ).

Mapping a Function

Mapping a function is when we place the input variable (x) and have it return * (times) the desired amount. #mapping #input or an argument ex: def function2 (x): return 2*x #calling it: a = function2 ( 3 ) : :what happened here is we assign an argument 3 to the function (mapping), and this will return 2*x or 2(times)3, it will equal ( 6 ) so when we call the variable A it will print 6.

Multiple arguments in the Function

We are defining a function called Function3 with two arguments X and Y, and let's have them return as X + Y. Ex : def function3 ( x, y ): return x + y e = function3 ( 1, 2) print (e) 3 we assigned the function3 to variable E with two arguments (1, 2) and when we print E we get 3.

Errors in Functions

there are possible Errors when calling a function: D = function2 ( ) TypeError: function2 ( ) missing 1 required positional argument. -- so basically we are missing the number or letter in-between the parenthesis


Ensembles d'études connexes

CHAPTER 42 EAQ - STUDY QUESTIONS

View Set

Ch. 14; HR: Employment Discrimination

View Set

Cartas sobre la ley y la gracia: Romanos y Galatas

View Set

Chapter 7: Guiding Reading Comprehension

View Set

Microeconomics: Technology, Production, and Costs

View Set