Using the Python Function
Before we discuss the process of creating and using functions, we should mention a few things about function names. Just as you name the variables that you use in a program, you also name the functions. A function’s name should be descriptive enough so anyone reading your code can reasonably guess what the function does.
In Python, a function is a block of code that performs a specific task and returns a result. Functions are an essential part of any programming language, as they allow you to break down complex tasks into smaller, more manageable pieces.
Most programs perform tasks that are large enough to be broken down into several subtasks. For this reason, programmers usually break down their programs into small manageable pieces known as functions. A function is a group of statements that exist within a program for the purpose of performing a specific task. Instead of writing a large program as one long 5 Functions it can be written as several small functions, each one performing a specific part of the task. These small functions can then be executed in the desired order to perform the overall task.
This approach is sometimes called divide and conquer because a large task is divided into several smaller tasks that are easily performed, one that uses a long complex sequence of statements to perform a task, and another that divides a task into smaller tasks, each of which is performed by a separate function. When using functions in a program, you generally isolate each task within the program in its own function. For example, a realistic pay calculating program might have the following functions:
To define a function in Python, you use the def keyword followed by the function name, a set of parentheses, and a colon. For example:
def greet(name):
print('Hello,'+name)
greet(Timi)
Output:
>>> Hello,Timi
This function, called greet, takes a single parameter called name and at the last line, the argument name was passed assigned a value “Timi” which prints out the greeting to the console with the name = “Timi.
To call a function in Python, you simply use the function name followed by a set of parentheses. For example:
greet('Alice') #prints "Hello, Alice"
Functions can also return a result using the return keyword. For example:
def add(x, y):
return x + y
result = add(1, 2) # result is 3
Output:
>>> 3
In this example, the add function takes two parameters, x and y, and returns the sum of these two values.
Functions can also have default values for their parameters, which are used if no value is passed when the function is called. For example:
def greet(name, greeting="Hello"):
print(greeting + ", " + name)
greet("Alice") # prints "Hello, Alice"
greet("Bob", "Hi") # prints "Hi, Bob"
In this example, the greet function has a default value of “Hello” for the greeting parameter. If no value is passed for this parameter, the default value is used.
The 2nd line prints out “greeting”, due to the default value “Hello” followed by the value passed to it in line 3 (“Alice”).
And also prints out “HI, Bob” in respect to the argument passed in line 1, name and greeting.
>>> Hello, Alice
>>> Hi, Bob
Functions are a powerful tool in Python, and they can greatly simplify your code by breaking down complex tasks into smaller, more manageable pieces. Whether you’re new to programming or an experienced developer, learning how to use functions effectively is an important skill to master.