How to use a function in python | Python tutorial for beginners
How to use a function in python | Python tutorial for beginners

How to use a function in python | Python tutorial for beginners

When a group of a certain number of related instructions are used together is called as functional in Python programming language. They are a reusable block of codes which are used to perform a specific task. functions are executed only to perform a specific task and they are specially called. Depending upon the task, it may carry out return value.

Including functions in a program helps to make it more organized and manageable. Especially, while working on a large program, having small code block can increase the readability of the code along with it improving the final results.

Defining functions in python

There are some set of rules which need to follow while defining a function:

  • Def keyword is used to initiate the function.
  • Def keyword is followed by a name of a function which is further followed by () containing the statement passed by the user and “:” is used at the end.
  • After putting the colon, the body function starts with an intended block in a new line.
  • The argument having a return statement sends a result object back to its caller and a return statement with no argument is equivalent to the return non-statement.

Syntex for writing a function in python programing

def (arg1, arg2, … argN):

return

Go ahead with you career by joining python course in Delhi provided by industry experts

How to call a function in python

Defining a function isn’t all we have to do so as to start utilizing it in our program. Defining a function just structures the code blocks and gives the function a name. To execute a function, we have to call it. Just when it is specifically called, a function will execute and give the necessary yield. Presently, there are two ways in which we can call a function after we have characterized it. We can either call it from another function or we can call it from the Python prompt.

Example

# defining a function

def printOutput( str):

#This function will print the passed string

print (str)

return;

#calling a function

Print output(“Welcome to AIDM”)

Output:

Welcome to AIDM

How to add Docstring in a Python Functions

The first statement or string in any Python function (optional statement) is called a docstring. It is utilized to quickly and freshly portray what a function does. ‘Docstring’ is the abbreviation for ‘documentation string’.

Despite the fact that remembering a docstring for our function is optional, it is viewed as a decent practise as it increases the readability of the code and makes it easy to understand. We utilize triple statements around the string to write a docstring. A docstring can also reach out up to numerous lines.

Boost your career as a python developer by joining the best python training institute in Delhi.

Join our online & offline java training institute in delhi and android app development course for beginners to become an expert in this field.

Example:

In the example accommodated calling a function, we utilized a remark to depict what the function would do. We will do likewise in this example as well. Just that, we will utilize a docstring here to show what the function will do.

# defining a function

def print output( str):

“’ This function will print the passed string’”

print (str)

return;

#calling a function

Print output(“Welcome to AIDM”)

Output:

Welcome to AIDM

The extent of a variable is a piece of the program where the variable is unmistakable.

As we have just talked about nearby and global variables in the Python Variables module of this instructional exercise, we realize that the variables characterized inside a function just have a neighbourhood scope. Which means, the variable characterized inside a function is just unmistakable inside that function.

It will be good if you learn Data Science course in delhi yourself by joining the Django training in delhi

def func():

x = 5

print(“value of x inside the function”, x)

#calling the function

x = 10

func()

print(“value of x outside the function”, x)

Output:

value of x inside the function 5

value of x outside the function 10

The lifetime of a variable is the timespan until when the variable exists in the memory. Variables characterized inside the function just exist as long as the function is being executed. Thus, the lifetime of a variable characterized inside a function closes when we come back from the function or when the control comes out of the function.

We have given a brief of the information about the function in python programming language. If you are still hungry to learn more, you can join our Python tutorial for beginners which is also available online.

For getting certified and having the best career growth, look at our Python training institute in Laxmi Nagar Delhi.

Recommended blog:

Data Science Course in Delhi Laxmi Nagar
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •