14. Introduction to Functions in Python- How to Create a Function and More about Return Statement

Опубликовано: 29 Сентябрь 2024
на канале: Learning With Faraz
63
4

In this video we are going to discuss about FUncitons in Python

IN General Language Functions mean "Work"

But in technical Terms and programming we define it as "A FUNCTION IS A MEANS OF PACKAGING UP AND PARAMATERIZED FUNCTIONALITY"

in order to create a function in Python the following Syntax Should be followed
def funtion_name( parameters ):
suite


the def keyword stands for definition and function_name here can be any python valid identifier , and Lower cases are considered for convince.
Parameters here can b of any number that we can pass to a function
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.
Defining a Function

You can define functions to provide the required functionality. Here are simple rules to define a function in Python.

Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).

Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.

The first statement of a function can be an optional statement - the documentation string of the function or docstring.

The code block within every function starts with a colon (:) and is indented.

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Syntax

def functionname( parameters ):
"function_docstring"
function_suite
return [expression]

By default, parameters have a positional behavior and you need to inform them in the same order that they were defined.
Example

The following function takes a string as input parameter and prints it on standard screen.

def printme( str ):
"This prints a passed string into this function"
print str
return

Calling a Function

Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code.

Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme() function −
Live Demo

#!/usr/bin/python

Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str
return;

Now you can call printme function
printme("I'm first call to user defined function!")
printme("Again second call to the same function")

When the above code is executed, it produces the following result −

I'm first call to user defined function!
Again second call to the same function




#FunctionsinPython #