Functions Flashcards
A function
A block of code that is only executed when it is called
Write the function to print Hello World!:
The functions name should be “start”
Def start():
Print(“Hello World!”)
What are arguments?
Information you send to a function
Return statement
Functions send Python values/ objects to the caller. These values/objects are known as the functions return value.
Keyword arguments
Arguments preceded by an identifier when we pass them to a function, the order of the arguments doesn’t matter, unlike positional arguments, Python knows the names of the arguments that our function receives.
Write a function with key word arguments for Alexander cardoza
The function looks like this
Def name(First,Last):
First +” “ + Last
name(“alexander”=First, “cardoza” = Last)
Output:
alexander Cardoza
Nested function calls
Function calls inside other function calls, innermost function calls are resolved first and the returned value is used as an argument for the next outer function
In the nested function
Print(Round(Abs(Float(Input(“enter number”)))))
What would happen if you inserted the number -6.9
The number 7 would be printed
Scope
The region that a variable is recognised
A variable is only available from the region it is created
global and locally scoped versions of a variable can be created.
Local scope
Available only in the function that it was created in
Global scope
Available inside and outside of functions as it was created outside of a function
L
E
G
B
Local
Enclosing
Global
Built-in
Kwargs
Parameter that will pick all arguments into a dictionary
-useful so that a function can accept a varying amount of keyword arguments
How do you use kwargs?
In the brackets of your function write “**variable” (variable can be anything)
And in the function’s block of code , where you want to insert the variable write the name of your variable and then use the square brackets [] to define which variable you would like to use e.g
Def printName(**kwargs):
Print(“Hello “ +kwargs[name]
printName(name= ‘alex’)
Key,value
The key has children: (the value)