Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

FunctionInPython

The document explains the concept of functions in Python, highlighting their definition, types (built-in and user-defined), and benefits such as reusability and modularity. It provides a syntax guide for creating functions, including examples of functions with and without parameters, as well as those that return values. Additionally, it describes how to call functions and includes examples demonstrating their usage.

Uploaded by

rathodevv6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

FunctionInPython

The document explains the concept of functions in Python, highlighting their definition, types (built-in and user-defined), and benefits such as reusability and modularity. It provides a syntax guide for creating functions, including examples of functions with and without parameters, as well as those that return values. Additionally, it describes how to call functions and includes examples demonstrating their usage.

Uploaded by

rathodevv6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MMN BCATY

2.3] Defining and calling functions, passing arguments to functions

What is Function?
 In Python, the function is a block of code defined with a name.
 We use functions whenever we need to perform the same task multiple
times without writing the same code again.
 It can take arguments and returns the value.
 Function improves efficiency and reduces errors because of the reusability
of a code.
 Once we create a function, we can call it anywhere and anytime.
 The benefit of using a function is reusability and modularity.

Python support two types of functions

1. Built-in function
2. User-defined function

Built-in function

 The functions which are come along with Python itself are called a built-in
function or predefined function.
 Some of them are listed below.
range(), id(), type(), input(), eval() etc.

User-defined function

 Functions which are created by programmer explicitly according to the


requirement are called a user-defined function.

Creating a Function

Use the following steps to to define a function in Python.

 Use the def keyword with the function name to define a function.
 Next, pass the number of parameters as per your requirement. (Optional).

Mr. D. S. Kiwde Page 1


MMN BCATY

 Next, define the function body with a block of code. This block of code is
nothing but the action you wanted to perform.
In Python, no need to specify curly braces for the function body. The
only indentation is essential to separate code blocks. Otherwise, you will get an
error.

Syntax of creating a function

def function_name(parameter1, parameter2):


# function body
# write some action
return value

Here,

 function_name:
Function name is the name of the function. We can give any
name to function.
 parameter:
Parameter is the value passed to the function. We can pass any
number of parameters. Function body uses the parameter’s value to
perform an action
 function_body:The function body is a block of code that performs some task.
This block of code is nothing but the action you wanted to accomplish.
 return value: Return value is the output of the function.
Note: While defining a function, we use two keywords, def (mandatory) and
return (optional).

Mr. D. S. Kiwde Page 2


MMN BCATY

Creating a function without any parameters

Now, Let’s the example of creating a simple function that prints a welcome
message.

# function
def message():
print("Welcome to Python")

# call function using its name


message()

Output

Welcome to Python

Mr. D. S. Kiwde Page 3


MMN BCATY

Creating a function with parameters

Let’s create a function that takes two parameters and displays their values.

In this example, we are creating function with two parameters ‘ name’ and ‘age’.

# function
def course_func(name, course_name):
print("Hello", name, "Welcome to MMN")
print("Your course name is", course_name)

# call function
course_func('DSK', 'Python')

Output

Hello DSK Welcome to MMN

Your course name is Python

Creating a function with parameters and return value

1. Functions can return a value.

2. The return value is the output of the function.

3. Use the return keyword to return value from a function.

Mr. D. S. Kiwde Page 4


MMN BCATY

Calling a function
Once we defined a function or finalized structure, we can call that function
by using its name.

We can also call that function from another function or program by


importing it.

To call a function, use the name of the function with the parenthesis, and if
the function accepts parameters, then pass those parameters in the parenthesis.

Example

def even_odd(n):
# check number is even or odd
if n % 2 == 0:
print('Even number')
else:
print('Odd Number')

# calling function by its name


even_odd(19)
# Output Odd Number

Mr. D. S. Kiwde Page 5

You might also like