Chapter - 3 - Working With Functions
Chapter - 3 - Working With Functions
1. Introduction – That means that a function is a piece of code written to carry out a specified task. To carry out that
specific task, the function might or might not need multiple inputs. When the task is carried out, the function can or
Built-in functions - such as min() to get the minimum value, print() to print an object to the terminal.
User-Defined Functions - which are functions that users create to help them out.
Anonymous functions - which are also called lambda functions because they are not declared with the
Functions vs Methods - A method refers to a function which is part of a class. You access it with an instance or
object of the class. A function doesn’t have this restriction: it just refers to a standalone function. This means that all
2. Defining Function is Python – The four steps to defining a function in Python are the following:
a. Use the keyword def to declare the function and follow this up with the function name.
b. Add parameters to the function: they should be within the parentheses of the function. End your line with a
colon.
d. End your function with a return statement if the function should output something. Without the return
3. Parameters vs Argument – Parameters are the names used when defining a function or a method, and into which
arguments will be mapped. In other words, arguments are the things which are supplied to any function or method
call, while the function or method code refers to the arguments by their parameter names.
4. Returning values from functions – Refer to below given example of User defined function:
Note that as you’re printing something in your hello() functions, you don’t really need to return it.
However, if you want to continue to work with the result of your function and try out some operations on it, you will
need to use the return statement to actually return a value, such as a String, an integer.
Consider the following scenario, where hello() returns a String "hello", while the function hello_noreturn() returns
Note that hello_noreturn() function does not have return statement it will return an object with value None.
Tip functions immediately exit when they come across a return statement.
5. Compositions – Function composition is the way of combining two or more functions in such a way that the output
of one function becomes the input of the second function and so on.
For Example -
First the add() function is called on input 5. The add() adds 2 to the input and the output which is 7, is given as the
defined outside have a global scope. That means that local variables are defined within a function block and can only
be accessed inside that function, while global variables can be obtained by all functions that might be in your script.
a. Function without return statement and default parameter – The following example shows how to use a
default parameter value. If we call the function without argument, it uses the default value:
Output –
Output –