Categories: Python

Python programming language Functions

Python programming language functions

In this tutorial, we will discuss the concept of Python programming language functions.

In python, function is a collection of related and reusable statements include as a single unit. This is used to perform a specific action that looks like a method.

Python programming language Functions

Basically two type of functions are available in python.

1. Built-in function – this functions defines and it is a built-in python programming language.
For Examples – Math function, String function
2. User- defined function – Users can define the function themselves.

This tutorial explains user defined functions

The advantages of using functions are:

  • clearly understanding code
  • reducing duplication in code
  • reuse of code
  • Information hiding


Syntax of python function

Syntax
About user-defined function
  • A function created with the def keyword in python that is used to identify the start of the function.
  • function_name is used to uniquely identify every function.
  • Parameters and arguments are used to pass variables and values to function but parameter passing is optional (it is not compulsory).
  • A colon mark is used to identify the end of the function.

Example of function

We can demonstrate about python function using the following examples

Program 1

How to create simple “hello world” program using python function

Example
When the above code is executed, it produced the following results.
Hello world


Program  2

Example
When the above code was executed, it produced the following results
hello i am a function
hello i am a function
hello i am a function

Program 3

When the above code was executed, it produced the following results

hello i am a function


Program 4

When the above code was executed, it produced the following results
hello mohan

hello kanthan
hello Nimal
hello Santhan

Program 5

When the above code was executed, it produced the following results
hay kamalan
Program 6
Python Functions
When the above code was executed, it produced the following results
10
Program 7

Function with argument

Python Functions
When the above code was executed, it produced the following results
10
62
67
60
Program 8
When the above code was executed, it produced the following results
(‘my first name is’, ‘logan’)
(‘my last name is’, ‘Mohan’)
(‘my age is’, 34)
(‘my sex is’, ‘M’)

The return ststement

We can use the return statement in Python
Syntex of return statement
return[expression_list]
Example 1
Example 2
When the above code was executed, it produced the following results
25
Example 3
When the above code was executed, it produced the following results
25
250

Default in python function

Program 1
Output
2 5
Program 2
Output
87 12.9 12 kannan
Program 3
Output
87 12.9 24 vannan
Find volume in a round shape
Output
33.5103216383
Suggested for you
Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.