Functions that are part of the Python language, e.g. input(), print(), int(), float(), str(), len(), sum(), min(), max()
Built-in functions
Some are specific to certain data types/structures, e.g. Strings (str.isupper(), str.lower(), str.count()), Lists (list.append(), list.sort(), list.index())
Some are only available as part of a module that you need to import, e.g. random.randint()
Different languages
Offer different built-in functions, often specialising in areas that the language is intended for
Process abstraction
Functions serve to abstract a process - they give you a way of performing a task without needing to know the details of how that task is done
Process abstraction
You don't need to know how an elevator works, just that you get in and press a button to be taken to the specified floor
You don't need to know how a car's accelerator works, just that you press it to make the car go faster
Using the sum() function
All you need to know is that if you pass it a list of numbers, it will return the total of them
Characteristics of good functions
Perform a well-defined task or process
Data the function needs should be passed in via parameters
If the function produces a result, it should return the result
Code inside a function should be independent of calling code
Functions should be modular, with clear input and output
The output of a function may become an input of another function
Function declaration
The code which defines a function so that it can be used ("called") later on
Declaring a function
1. Specify a function name
2. Details of any parameters that can be passed to it
3. The lines of code that the function performs when it is called
4. May include return statements to return a result back to the program
Declaring a function in different languages
PHP: function repeat($text, $count) { ... }
Java: public static String repeat(String text, int count) { ... }
Python: def repeat(text, count): { ... }
Functions should be declared at the start of a program, and then called where they're needed
Declaring the "repeat" function in Python
1. Define "repeat" function (receives "text" and "count")
2. Set "result" to an empty string
3. Repeat "count" times
4. Concatenate "text" to the end of "result"
5. Return "result"
Pseudocode and flowcharts can be used to illustrate the flow of programs and their control structures, including function calls
Scope
The part of a program in which a variable exists and can be referred to
Local variable
A variable that is created inside a function, whose scope is limited to that function
Global variable
A variable created outside of a function, which can (but shouldn't) be accessed from any function
Trying to assign a value to a global variable will create a local variable instead, unless declared as a global in the function
You should avoid referring to global variables from inside functions
Parameters
Define what data can be passed into the function when it is called
Optional parameter
A parameter that can be given a default value
The parameter names specified when defining a function become local variables in the function when it is called
Only the value of a variable is passed to/returned from a function, not the variable itself
Return statement
Allows data to be returned from a function back to the code that called it
Local variables
Variables in the function when it is called
Parameter
Can be made optional by giving it a default value
Defining a function
1. Local variables are created
2. Values of parameters are provided when the function is called
Repeat function
def repeat(text, count = 2):
result = ''
for num in range(count):
result = result + text
return result
You must provide a value for each non-optional parameter
print(repeat('Hello!', 4))
word = 'Duck, '
phrase = repeat(word) + 'Goose!'
print(phrase)
Parameter scope
Parameters of a function are local variables, they only exist within the function
Only the value of a variable is passed to the function / returned from the function
dec_to_percent function
1. if num > 1:
2. num = '100%'
3. elif num < 0:
4. num = '0%'
5. else:
6. num = str(round(num * 100)) + '%'
7. return num
Assigning a value to num in the function does not change num in the main program - they are different variables
Return statement
Allows us to return data from a function back to the code that called it
A function can contain multiple return statements, the function will end and return the value of the first return statement encountered
get_grade function
if mark >= 80:
return 'HD'
elif mark >= 70:
return 'D'
elif mark >= 60:
return 'CR'
elif mark >= 50:
return 'P'
else:
return 'F'
A function that returns a result can be used wherever using a value of that type would be valid
Functions return values, not variables. It is up to you to use the value as needed.