Unit 7 : Python Function Flashcards
List out the types of function ( 2 )
- Built-in functions provided by python ( input() , type() )
- Functions that we define ourselves and use
What should we add when creating a function?
- Define a function using def keyword
What is a function?
- Some reusable code that takes arguments as input does some computation and then returns a results or results
How can we call / invoke the function ?
- By using function name, parenthesis and arguments in an expression
What should we do when we are adding code to the function body?
- Indent the body
Will the function be executed like this?
def print_lyrics():
print (“I’m a lumberjack, and I’m okay. “)
- No , it wasn’t called
- A functions is a stored and reuse pattern
What is void functions also called as?
- Non-fruitful Functions
What is a fruitful function?
- Functions that return value
Come out an example of the pesudocode of function
Start
Prompt user for N
Read N
Call AVRG(10,5,N)
End
Function AVRG(n1,n2,n3)
Calculate sum = n1 +n2 + n3
Calculate result = sum/3
Print result
EndFunction
What does the return statements do?
- Ends the function execution and “sends back” the result of the function
Come out an example of the pesudocode of function ( with return value )
Start
Prompt user for N
Read N
Call AVRG(10,5,N) returning average
Print average
End
Function AVRG(n1,n2,n3)
Calculate sum = n1 +n2 + n3
Calculate result = sum/3
return result
EndFunction
What is an arguments?
- Is a value we pass into the fucntion as its input when we call the function
When we put arguments?
- Put arguments in parenthesis after the name of the function
Why should we use arguments?
- So that we can direct the function to do different kinds of work when we call it at different times
greet(“Hello”#This is arguments )
What is a parameter?
- A variable which we use in the function definition that is a “handle” that allowd the code in the function to access the arguments for a particular function invocation
def greet(message # This is parameter )