You may be faintly familiar with functions by constantly using the built-in functions that Python provides. For example print(), input(), etc. are some of the built-in functions in Python.
A function consists a block of code with which we want to perform some kind action. Now instead of writing that particular code repeatedly when a certain action is required, we can just call the function. This promotes code reusability and modularity.
Functions help us to write clean codes. The code we write becomes more readable. Apart from built-in functions provided by Python, we can create our own functions these functions are called user-defined functions.
def functionname( parameters ): function_body return (expression)
You have to define the functions and provide them the required functionality. You define a function by using the syntax given and writing the logic in the function body. Here are some rules to define a function in Python.
- The function begins with the keyword def followed by the function name and parentheses ( ).
- You should put all your input parameters or arguments within these parentheses. You can define parameters inside these parentheses.
- The function starts with a colon (:). The code block within every function is the function body. The function body is indented to show that the function is defined.
- The statement return (expression) exits a function. The keyword return also helps in passing back an expression to the caller of the function. A return statement with no arguments or expression to pass is the same as return None.
When we define a function we only give it a name, specify the parameters that we want to be included in the function and write the blocks of code which we call functions body. Once the basic definition of a function is completed, you can execute the function by calling it from some other function or directly from the Python prompt.
# Defining function here def displaynum ( num ): # notice the keyword def, function name displaynum and a number parameter num #function body below print("Number passed in the function is ", num) return
The above block of code defines the function displaynum. Now in order to execute the function, we will call it.
# Calling the function n = 12 b = 12.5 displaynum ( n ) displaynum ( b )
After code execution, the result is –
Number passed in the function is 12 Number passed in the function is 12.5
Many times we come across a situation where we want a default value to be taken if the user has not input any value from their side. A default argument comes to rescue in these situations if we are defining a function. A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.
def student ( name, marks = 90 ): print ( "Name: ", name ) print( "Marks: ",marks ) return # function call nm = "Charles" student(nm, marks = 95) student (nm) #default value taken
After code execution, result is –
Name: Charles Marks: 95 Name: Charles Marks: 90