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.

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 usingthe 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
When the above code was executed, it produced the following results
10
Program 7
Function with argument
Python
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

Share
Published by
Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.