Functions in Python | Introduction | Python for beginners #lec56

Jenny's Lectures CS IT
6 May 202324:07

Summary

TLDRThis video script introduces the concept of functions in Python programming. It explains the necessity and benefits of using functions through real-life analogies, such as sharing a recipe or a mother's multitasking. The script outlines how to define and call functions, emphasizing code reusability, readability, and time-saving aspects. It also distinguishes between built-in and user-defined functions, providing examples and demonstrating the process with a practical coding exercise in Reebok's Blockly World.

Takeaways

  • 😀 Functions in Python are blocks of code that perform specific tasks when called.
  • 🔑 Functions help in organizing code by grouping related statements together, making the code more modular and easier to maintain.
  • 🍲 The analogy of a recipe for idli is used to explain the concept of functions, where the recipe (function) can be followed (called) multiple times to achieve the same result.
  • 👩‍🏫 The importance of functions is emphasized by showing how they can save time and effort by avoiding the repetition of code.
  • 📝 The definition of a function in Python uses the 'def' keyword, followed by the function name and parentheses, which may include parameters.
  • 🔄 Functions can be called with parameters to perform different tasks based on the input, demonstrating their versatility.
  • 🔧 Built-in functions are pre-defined in Python and can be used directly, such as 'print', 'len', 'max', and 'min'.
  • 🛠 User-defined functions are created by the programmer to perform specific tasks as needed, enhancing the functionality of the program.
  • 📈 The benefits of using functions include increased code reusability, improved readability, and time-saving by avoiding code duplication.
  • 🤖 A practical example using a robot in Reebok World is provided to illustrate how defining and calling functions can control actions and simplify programming tasks.

Q & A

  • What is the main topic discussed in this video script?

    -The main topic discussed in this video script is functions in Python programming language.

  • Why are functions important in programming?

    -Functions are important in programming because they promote code reusability, increase code readability, and save time by avoiding the need to repeat code blocks.

  • Can you give an example of a real-life analogy for functions mentioned in the script?

    -The script uses the analogy of a recipe for making idli, a South Indian food, to explain the concept of functions. Just as a recipe can be written down and followed multiple times without repeating the steps, functions in programming can be defined once and called multiple times to perform a specific task.

  • What are the two major steps involved in using a function in Python?

    -The two major steps involved in using a function in Python are defining the function using the 'def' keyword and then calling the function by its name followed by parentheses.

  • What is the purpose of the 'def' keyword in Python?

    -The 'def' keyword in Python is used to define a function, marking the start of the function's declaration.

  • What is the term used for functions that are already defined in Python?

    -The term used for functions that are already defined in Python is 'built-in functions'.

  • How does defining a function help in reducing the number of lines in a code?

    -Defining a function helps in reducing the number of lines in a code by allowing you to encapsulate a block of code that performs a specific task and then reuse that block by simply calling the function, instead of repeating the code block multiple times.

  • What is the significance of indentation in Python function definitions?

    -Indentation in Python function definitions is significant because it demarcates the body of the function, indicating which lines of code are part of the function's execution block.

  • What is the difference between built-in functions and user-defined functions in Python?

    -Built-in functions are pre-defined in Python and can be used directly without any additional code. User-defined functions, on the other hand, are created by the programmer to perform specific tasks as per the program's requirements.

  • Can a function in Python accept parameters?

    -Yes, a function in Python can accept parameters. Parameters are specified within the parentheses in the function definition and can be used to pass values into the function to make it more versatile.

  • What is the purpose of the 'return' statement in a Python function?

    -The 'return' statement in a Python function is used to send a value back to the caller of the function. It is optional and not all functions need to return a value.

Outlines

00:00

📚 Introduction to Functions in Python

The speaker begins by transitioning from a previous Python project, the Hangman game, to a new topic: functions in Python. The video aims to explain the concept of functions, their necessity, and benefits. A real-life analogy is introduced to explain functions, comparing them to a recipe for making idli, a South Indian dish. The analogy illustrates how sharing a recipe (function) multiple times can be streamlined by writing it down once, emphasizing the idea of reusability and efficiency. The speaker also hints at introducing a new tool in the video that will help viewers become more comfortable with functions.

05:01

🛠 Defining and Using Functions

This paragraph delves into the technical aspects of defining and using functions in Python. The speaker explains the syntax for defining a function using the 'def' keyword, followed by the function name and parentheses. Parameters and return statements are introduced as optional components of a function. The importance of indentation in Python is highlighted. The concept of built-in functions, such as 'print', is contrasted with user-defined functions, which are created as per specific needs. The speaker emphasizes the benefits of using functions, such as code reusability, readability, and time-saving, using the example of a simple greeting function.

10:01

💡 Benefits of Functions and Practical Example

The speaker discusses the advantages of using functions, such as increased code reusability, readability, and time efficiency. A practical example is provided to demonstrate how defining a function can reduce the number of lines of code and make the program easier to understand. The example involves creating a Python file named 'intro.py' and defining a simple function called 'greet' that prints a greeting. The speaker then shows how to call this function multiple times in the program, thereby avoiding the repetition of code.

15:03

🤖 Applying Functions in a Coding Tool

The speaker introduces a coding tool, Reebok's World, to provide a visual and interactive way to understand functions. The tool allows users to write Python code and execute commands to control a robot. Built-in functions like 'move' and 'turn left' are used to give commands to the robot. The speaker then demonstrates how to define a custom function, 'turn right', to make the robot turn right by using the 'turn left' function three times. This practical demonstration helps to reinforce the concept of functions and their application in programming.

20:05

🔄 Defining Custom Functions for Robot Commands

In this paragraph, the speaker encourages viewers to define their own function, 'turn around', for the robot in the coding tool. The function is defined by turning left twice, which effectively makes the robot turn around. The speaker then integrates this custom function into the robot's sequence of commands, demonstrating how it simplifies the code and makes it more efficient. The video concludes with an exercise for viewers to practice defining and using functions, emphasizing the importance of understanding functions in programming.

Mindmap

Keywords

💡Function

In the context of the video, a 'function' refers to a block of code or a set of statements in Python that perform a specific task when called. It is a fundamental concept in programming that allows for code reusability, making the script more efficient and easier to manage. The video script uses the concept of a recipe for making idli as an analogy to explain how a function groups a set of steps to be reused without repetition.

💡Defining a Function

The process of 'defining a function' in Python involves using the 'def' keyword, followed by the function name and parentheses. This is a crucial step in creating reusable code blocks. In the script, the process is explained with the simple example of a 'greet' function, which when called, prints a greeting message, demonstrating how functions encapsulate behavior.

💡Function Call

A 'function call' is the act of executing a function by referring to its name followed by parentheses. This is how the predefined behavior within a function is invoked. The script illustrates this with the 'greet' function, where calling the function results in printing the greeting, showing the practical application of functions in a program.

💡Parameters

Parameters are optional values that can be passed to a function when it is called, allowing the function to behave differently based on the input. The script does not use parameters in its initial examples but explains their concept, noting that functions like 'print' use parameters to determine what to display.

💡Return Statement

The 'return statement' in a function is used to send back a result to the caller. It is optional and not all functions need to return a value. The script mentions that functions can have a return statement to output a value or result of the function's computation, but it also clarifies that this is not mandatory.

💡Code Reusability

The concept of 'code reusability' is highlighted as one of the key benefits of using functions. It refers to the ability to use the same block of code multiple times without rewriting it. The script explains this with the idli recipe analogy and the 'greet' function example, emphasizing the efficiency gained by avoiding repetitive code.

💡Readability

'Readability' in programming refers to how easy the code is to understand. The script points out that using functions can improve the readability of code by reducing the number of lines and providing clear, descriptive function names that indicate the block's purpose, such as the 'greet' function used in the examples.

💡Built-in Functions

The script introduces 'built-in functions' as pre-defined functions in Python that are ready to use without any additional definition. Examples given include 'print', 'len', 'max', and 'min', which are used throughout programming to perform common tasks, demonstrating the convenience and time-saving aspect of these functions.

💡User-Defined Functions

In contrast to 'built-in functions', 'user-defined functions' are those created by the programmer to perform specific tasks tailored to their needs. The script explains how to define such functions using the 'def' keyword and provides examples like the 'greet' and 'turn right' functions, showing how they can be customized for particular behaviors.

💡Indentation

In Python, 'indentation' is syntactically significant and used to define the scope of code blocks, including functions. The script emphasizes the importance of proper indentation after the 'def' keyword and within the function body to avoid errors and ensure the function is defined correctly.

💡Reebok's World

Reebok's World, mentioned in the script, is a tool or platform for learning programming concepts through interactive exercises. It is used as an example to illustrate how functions can be applied in a practical and visual manner, with the script showing how to define and call functions within this tool to control a robot's movements.

Highlights

Introduction to the concept of functions in Python programming.

Explanation of why functions are necessary in programming.

Real-life example of a function using a recipe for making idli.

Discussion on the benefits of using functions in programming.

Definition of a function in Python and its syntax.

Mandatory and optional parts of a function definition.

How to call a function in Python.

Difference between built-in functions and user-defined functions.

Practical demonstration of defining and calling a function.

Introduction of a new tool for learning functions: Reebok's World.

Using built-in functions in Reebok's World to control a robot.

Defining a custom function 'turn right' in Reebok's World.

Advantages of functions in terms of code reusability and readability.

Practical exercise on defining a 'turn around' function.

Final thoughts on the importance and utility of functions in programming.

Transcripts

play00:00

so in the series of learning Python

play00:02

programming language in the previous

play00:03

video we have seen a project we have

play00:05

created a project Hangman game right so

play00:07

from this video I'm going to start a new

play00:09

topic that is functions in Python right

play00:12

and this will in this video we'll be

play00:14

discussing basic about function what is

play00:15

a function why basically we need a

play00:17

function this answer of this question is

play00:19

very important y functions what are the

play00:21

benefits of using function how to define

play00:23

a function what are different types of

play00:24

function

play00:25

right with the help of real life example

play00:28

I'll tell you what is function and with

play00:30

the help of program also practical also

play00:32

I will show you and I will introduce you

play00:34

with a new tool also in this video

play00:37

after using that tool you will be you

play00:39

know you know uh very friendly you can

play00:43

say with functions

play00:44

right it would be very easy for you guys

play00:46

maybe someone of you are aware about

play00:48

functions they have learned other

play00:50

languages CC Plus for Java but let's

play00:54

start this with a real life example

play00:56

suppose I know I know a recipe I know

play00:58

how to make idli one of my favorite from

play01:00

South Indian food actually

play01:02

and suppose one Sunday my friend came to

play01:05

me came to visit me and I made idli for

play01:08

them

play01:09

and she was like oh wow Italy like they

play01:12

they are so delicious and amazing so

play01:15

she asked me the recipe for that idli

play01:18

and I told her like like this

play01:22

even to be very Frank I don't know what

play01:25

is the recipe but let's take let's

play01:27

assume I know so Suppose there are these

play01:31

five or six or seven steps one two three

play01:33

four five six seven steps

play01:36

in the recipe so I told her these seven

play01:39

steps right maybe uh on next day or on

play01:43

another friend came to me and

play01:47

She also asked me for the same recipe

play01:49

because I made idli again

play01:51

because in this I am expert you can say

play01:53

so I told her the same recipe like this

play01:58

maybe every Sunday or every after two to

play02:01

three days someone is coming to me and I

play02:03

am telling them the recipe I am

play02:05

repeating these seven steps again and

play02:07

again I'm telling them the recipe right

play02:09

so

play02:10

what I can do rather than this what I

play02:13

can do now I'm feeling very frustrated

play02:15

after telling the recipe again again and

play02:17

again so what I can do I can just write

play02:19

down this recipe on a paper right

play02:23

and like r e c i p e

play02:27

recipe

play02:28

of idli you can say making idli right

play02:31

and whenever anyone comes to me and asks

play02:33

for the recipe I just hand over this

play02:35

piece of paper too

play02:37

damn right

play02:38

so yeah it is going to say what what is

play02:41

the benefit my energy and my time

play02:44

right I have just

play02:46

grouped these steps

play02:50

in US

play02:51

you can say an entity this is an entity

play02:53

on a paper and I'll just hand over that

play02:55

paper too

play02:57

whoever is going to ask me the recipe

play02:59

about the recipe

play03:00

right so yeah this is beneficial for me

play03:02

right same

play03:06

we have some function like uh in

play03:09

programming also we have to do something

play03:11

like some task again and again

play03:13

right suppose let's take one example

play03:17

and see if you take one more example

play03:19

like we human being also we are

play03:21

performing different different

play03:22

functionality we are multitasker like

play03:24

let's take a female as a mother as a

play03:27

wife as a daughter as a working woman

play03:29

and many functionalities are there

play03:31

right so different different

play03:32

functionalities are there for the same

play03:34

entity for the same person right means

play03:38

like if you are in front of your student

play03:40

you are working you are you know uh you

play03:43

know your responsibilities work you know

play03:45

acting as a teacher right if you are in

play03:48

front of your

play03:49

children then your responsibility means

play03:51

your functionality is your working as a

play03:53

mother something like this

play03:56

so whatever is in front of you according

play03:58

to that you have to perform your task

play04:01

right for different different inputs you

play04:04

are performing your specific task

play04:06

right so like this example also you can

play04:10

relate with the functions let's take uh

play04:13

this example if in programming also like

play04:17

two lines there are two lines simplest

play04:19

example I am taking like print hi and

play04:23

like Jenny

play04:26

and good morning

play04:29

like this

play04:30

I want to print these two lines again

play04:32

and again in my program like perform

play04:34

some specific task then execute these

play04:37

line then perform some specific task

play04:39

then again I want to

play04:41

before I want to you know greet like I

play04:45

want to uh print hi Jenny good morning

play04:47

then again perform some specific task in

play04:49

the same program again I want to perform

play04:51

these two lines perform some specific

play04:53

tasks again I want to greet like hi

play04:55

Jenny good morning right in program

play04:57

maybe 50 times so 50 times you need to

play05:00

write down these two lines so rather

play05:02

than this what we can do rather than

play05:03

repeating the two lines again and again

play05:05

what we can do the same thing

play05:09

just wrap up this thing

play05:12

here

play05:14

like print these two lines print

play05:17

then Jenny and

play05:20

good morning these two lines here

play05:22

right and just call it what I'm calling

play05:25

this a recipe right so it's like once

play05:28

you're functioning so let's call this

play05:30

thing what a function name greet

play05:35

and whenever you want to execute the

play05:37

these two lines then just call this

play05:40

function greet greet greet so rather

play05:42

than writing these two lines again and

play05:45

again just have to write down simple

play05:47

function name greet that's it

play05:49

right now I'll tell you how to define a

play05:52

function how to call the function there

play05:53

are two major steps basically first of

play05:56

all you have to define a function then

play05:57

you have to call the function right

play06:00

so now I hope you got it what is

play06:02

basically a function function is just

play06:04

what a block of statements or block of

play06:08

code this is just a block of code this

play06:10

is just a block of code or statement

play06:12

these are statements

play06:13

right

play06:15

which perform some specific task

play06:18

when it is cold it is going to perform

play06:22

those tasks only when you are going to

play06:24

call that function these lines are very

play06:26

important so when it is code so function

play06:30

is a group of

play06:32

you can say it's or you can say block of

play06:34

statements or block of code which is

play06:36

going to perform a specific task which

play06:37

perform a specific task when it is

play06:40

called that is function definition very

play06:42

simple right now how to define a

play06:46

function let me tell you two steps I

play06:48

have told you first Define a function

play06:49

then call the function right so how to

play06:52

define a function simple what is the

play06:55

syntax you will write down this def

play06:57

keyword def then

play07:00

here you will write down your function

play07:02

name any name you can take like add

play07:04

subtract grid any function name display

play07:06

anything

play07:07

these parenthesis then colon or in these

play07:12

parenthesis

play07:14

if you want to pass you can pass

play07:17

parameters right then here we have

play07:23

here we have function body or let me

play07:25

just write down this thing here we have

play07:26

function body

play07:28

and after that we have like return

play07:32

return statement

play07:35

return keyword and then some expression

play07:38

or whatever you want to return so this

play07:39

is what the complete one

play07:42

it is not necessary to pass parameter

play07:44

every time it is not necessary that

play07:46

every function is going to return

play07:47

something so this line is optional this

play07:50

one these parameters are optional but

play07:52

the mandatory part is to define a

play07:54

function def keyword function name these

play07:57

parenthesis maybe these are empty this

play08:00

colon and then function body

play08:02

right these are important these are you

play08:05

can say mandatory part if you are going

play08:07

to define a function parameters return

play08:09

expression these are optional part right

play08:11

and this this this should be indented

play08:16

right see I have there are some space

play08:20

at the beginning of these two lines so

play08:22

they should be indented otherwise it

play08:23

will give error

play08:25

right so this is you are going to define

play08:27

a function how to call the function then

play08:28

just write down the function name

play08:31

whatever the function name and

play08:34

these parenthesis if if you have passed

play08:39

some parameter if you use some parameter

play08:41

when you are defining a function then

play08:43

you have to pass those parameters here

play08:45

One Two Three or how many parameters you

play08:48

have

play08:48

there are no parameters just function

play08:50

name and these parenthesis this is what

play08:52

you are going to call the function

play08:53

right

play08:55

now

play08:56

and function will only work when you are

play08:58

going to call that function only

play09:00

definition of this function this is not

play09:02

going to work

play09:03

you have to call that function

play09:06

right like me as a teacher

play09:09

so whenever I am in front of my student

play09:11

right in front of my class

play09:14

then only I lacked as a teacher means at

play09:18

that time students are calling me you

play09:19

can say they are calling me so at that

play09:22

time only I'll work as a teacher right

play09:25

so you have to call that function

play09:27

right now there are two types of

play09:28

function built-in function plus user

play09:30

defined function so built-in functions

play09:33

are so built-in functions are already

play09:36

defined in Python already pre-coded or

play09:38

already defined right you don't need to

play09:40

write down any code to define those you

play09:42

can only use the you can directly use

play09:44

those function like simplest one is

play09:47

print in every program we use this

play09:49

function

play09:50

this is already defined in Python so you

play09:53

just have to use that function just

play09:55

print in these parenthesis whatever you

play09:58

want to print that you can pass here we

play10:00

pass that parameter the argument right

play10:04

built in like print we have Len function

play10:06

we have used this is also built in Max

play10:09

Min and type also we have used

play10:13

right

play10:14

so many functions are there you can also

play10:17

write down few built-in function in

play10:19

comment section

play10:20

now next a user defined a function user

play10:22

defined means you have to Define user

play10:24

will define those functions as per our

play10:26

need

play10:27

like whatever you want like I want to

play10:29

define a function of addition of two

play10:31

number

play10:31

so according to my head I'll Define a

play10:33

function the statement the code I'll

play10:35

write according to myself right and how

play10:37

to define that I have told you how to

play10:39

call that I have told you

play10:41

right

play10:42

now let's see this thing with the help

play10:44

of example and I will introduce you with

play10:46

a new tool also then you will get it

play10:47

better what function is and why we use

play10:50

it and now the the benefits or advantage

play10:53

of using function is what

play10:55

I guess now you got it what can be the

play10:57

benefits of using the function

play11:00

definitely your

play11:03

code becomes

play11:05

more reusable rather than writing these

play11:07

two lines again and again in the program

play11:09

just call the function so it means we

play11:12

are reusing these two lines so code

play11:14

reusability will increase

play11:16

the code would be more readable right

play11:19

because obviously if you call a function

play11:22

if you use functions then the number of

play11:25

lines in your code will also is going to

play11:27

decrease will be less and if less line

play11:30

of lines of code then your code would be

play11:32

more you can say readable

play11:35

right

play11:36

so these are few you can say and it will

play11:40

be time saving also right just Define

play11:42

one time and use reuse those thing again

play11:45

and again so it would be

play11:46

time saving for you right let me just

play11:50

show you this thing with the help of

play11:52

practical we'll see a program on

play11:54

functions okay so let's create a new

play11:57

file here for functions and

play12:02

functions like intro Dot py

play12:06

right

play12:08

and

play12:10

let's define a function depth how to

play12:13

define depth let's take a function

play12:15

suppose grid

play12:17

right colon and here it should be

play12:20

indented and there I just want to print

play12:23

high in one line and suppose here I want

play12:27

to print only gen

play12:29

these two lines

play12:30

so now

play12:32

this is definition of the function we

play12:34

are not passing any parameter because

play12:35

these are optional we are not returning

play12:37

anything here right so

play12:40

now here what we can do how to execute

play12:43

this function if I run this code see

play12:45

suppose we have defined a function

play12:47

and let's let's run this code

play12:49

see it is not printing anything the

play12:52

screen is blank right why so because

play12:56

to execute the function

play12:59

I have told you in the definition the

play13:01

function is a statement or block of

play13:03

statement block of code which performs

play13:05

some specific task when it is called so

play13:08

you have to call the function have to

play13:09

call the function just

play13:11

name of the function grid that's it this

play13:14

is calling of function now let's run

play13:15

this and this time it will print hygiene

play13:18

so if you want to print this multiple

play13:20

times

play13:21

so again I want to call grid again like

play13:25

read three times so these two lines will

play13:27

be printed three times in your program

play13:30

in the output see hi Jenny hi Jenny hi

play13:33

Jenny right

play13:36

so this is how the function will work

play13:39

now let me just introduce you with a new

play13:42

ah like tool we have Reeboks World our

play13:45

website is there okay for that you can

play13:48

directly search for like python coding

play13:50

free box world so

play13:53

first we'll take this example the second

play13:55

link alone World info python blocky this

play13:58

keep editor visible right let's run this

play14:01

and this is going to load

play14:04

right so here this type of thing would

play14:07

be opened here the project name is C

play14:09

along right

play14:12

okay now see the python code here we can

play14:14

write down python code so see here we

play14:16

have some functions like when you click

play14:20

on this Rebox keyboard then you will get

play14:22

some function like you can say command

play14:24

move turn left take put these kind of

play14:27

commands you can give these commands to

play14:29

this robot and it will perform according

play14:31

to your given command so here I have

play14:33

written one command like move here we

play14:35

have one command move movements it will

play14:37

move further One Step only so how to run

play14:39

this there's a play button right here

play14:42

just click on that and see

play14:45

it has moved one step ahead so last

play14:47

instruction completed so if you want to

play14:49

move this three steps or two steps then

play14:51

just use this function again move

play14:56

or maybe three times move

play14:59

and now if you want to go back here just

play15:02

click on this just go back this one and

play15:06

just click on the play so see it has

play15:09

moved three steps the robot so last

play15:11

instruction completed right so here we

play15:13

can write down our python codes we

play15:15

haven't written till now we are still

play15:17

you we are using those uh you know

play15:19

inbuilt function only we have we haven't

play15:22

defined our own function till now but we

play15:24

are going to Define right

play15:26

if one by one if you want to execute the

play15:28

instruction suppose first instruction

play15:30

instruction first and second and third

play15:31

so you have to click on this one step

play15:34

through button step

play15:36

click one then

play15:38

see it is going to show this one line

play15:40

would be executed move

play15:43

see like this

play15:45

again move again click move

play15:48

three line would be executed and that's

play15:51

it last instruction completed so let's

play15:54

back here

play15:56

and okay

play15:58

turn left there is one function use this

play16:01

turn left just click on that and it

play16:03

would be here turn left

play16:05

or rather than first turn left first we

play16:09

move this robot like three times and

play16:11

then we use the function

play16:14

turn left

play16:17

not at the starting

play16:21

so if I run this code here so three step

play16:24

then turn left

play16:26

see turn left the robot has turned left

play16:30

this weight is going then turn left this

play16:32

side

play16:33

and again

play16:35

if I

play16:39

after turn left if I use the move

play16:41

function again three times and if I run

play16:43

this again

play16:47

see now this would be executed move

play16:50

first three times turn left and move

play16:52

again

play16:53

three times right but there is no see

play16:57

there is no turn around or turn right

play17:00

function here

play17:01

right only turn left is there now if you

play17:04

want to

play17:06

here

play17:07

command the robot to turn right then how

play17:10

you leave that thing

play17:12

so Define your own function turn right

play17:15

function at this point of time if I want

play17:17

to give the command to this robot turn

play17:18

right

play17:19

then how will you now you have to Define

play17:22

your own function now pause the video

play17:23

and Define your own function

play17:30

okay how we are going to Define

play17:32

def keyword function name I am taking

play17:35

turn right

play17:37

parenthesis colon and now the definition

play17:39

it should be indented right

play17:43

now turn right means simply at this

play17:46

point of time if this robot we give this

play17:49

robot three times turn left command then

play17:52

it would be

play17:54

automatically at the last

play17:56

ultimately turn right command would be

play17:58

executed so three times turn left so now

play18:01

this is a turn right function now now if

play18:04

I call that this function here

play18:09

turn right

play18:12

and suppose again

play18:14

move

play18:15

One Step move and if I run this let me

play18:19

just stop this just click on this

play18:22

reload button and play

play18:29

like this

play18:30

right turn right and then one move but

play18:34

we have defined our own function suppose

play18:37

if you want to

play18:39

execute this if you want to command

play18:41

command this uh you know uh

play18:43

robot something like this okay the same

play18:46

thing then

play18:48

it has to move downward three step

play18:52

right again and then

play18:55

simply

play18:56

the face would be in this in this

play18:59

Direction only this direction forward

play19:01

Direction only right so how we are going

play19:04

to give this this this command see now

play19:08

at this point of time also

play19:10

for going downward you have to give the

play19:13

command obviously turn right like this

play19:16

we are facing then at this point of time

play19:18

means turn right right and if suppose

play19:21

you haven't defined this function

play19:24

then for turn right you have to write

play19:26

down this turn left three times

play19:29

so here rather than turn right you have

play19:32

to write down these

play19:38

three times

play19:40

then again move then again turn right so

play19:45

again write down these three commands

play19:47

turn left command here like three times

play19:52

here there should be no indentation

play19:54

right

play19:58

now three times move

play20:01

right and then finally

play20:05

turn left

play20:08

right

play20:10

this would be the case if we are not

play20:12

using the function turn right we haven't

play20:15

defined the function so let's stop this

play20:17

and now let's run this

play20:31

so now you are repeating these turn left

play20:34

and turn left these three lines again

play20:37

and again whenever you want to give the

play20:39

command to the robot turn right then

play20:40

three times you have to write down turn

play20:42

left again and again

play20:44

so better to define a function so that's

play20:46

exactly we have done just Define a

play20:47

function turn right and rather than

play20:49

these three lines

play20:51

just use one line turn

play20:54

right that's it rather than these three

play20:58

lines

play21:00

turn

play21:01

right

play21:03

just one line

play21:05

so yeah this is going to reduce the

play21:08

number of lines in your code when you

play21:09

are using function code reusability will

play21:12

increase and as well as code readability

play21:14

will also increase right this is also

play21:17

going to perform the same task

play21:19

one by one we are going to run this now

play21:21

this step through option

play21:23

let's run this once click then again

play21:26

click

play21:27

then this move command would be executed

play21:30

then second move again run again

play21:33

turn left again again again click on

play21:37

that now see cursor is a

play21:40

here at this function turn right now

play21:44

click on the step function now person

play21:46

would be here

play21:49

in the definition of this function so

play21:51

first turn left

play21:52

again second turn left again third turn

play21:55

left would be performed now that's it

play21:58

now we are we are turning we have turned

play22:01

right this robot then move then again

play22:04

turn right then again function would be

play22:06

cold see the cursor is again the control

play22:08

is again here within the function

play22:11

then three times turn left and then move

play22:15

then this move then this then again turn

play22:18

left and that's it

play22:20

right

play22:22

so this is beauty of function you can

play22:25

say now one exercise for you is

play22:28

turn around

play22:30

there is no function turn around here so

play22:32

you have to Define your own function how

play22:34

to turn around

play22:35

let's suppose move this robot three

play22:38

times here

play22:40

three times forward then turn around

play22:43

then three times backward the same

play22:45

position how you will Define your

play22:47

turnaround function pause the video and

play22:49

try this out

play22:56

okay I hope you have done this how to

play22:58

define turn

play23:01

I think this is very simple if you have

play23:02

done this turn left turn around is very

play23:04

simple

play23:05

just two times turn left and that's it

play23:08

this should be turn around two time turn

play23:11

left only right so let's

play23:16

here we will do that thing after turning

play23:19

left at the final time then we will call

play23:23

turn

play23:25

around function

play23:28

and then again

play23:30

move

play23:33

right let's run this and see what output

play23:36

you will get

play23:44

C

play23:46

turn around and then back one step

play23:49

so this is what we can Define our own

play23:52

function turn around turn right function

play23:54

so I hope you got the basics of

play23:56

functions why we need functions use of

play23:59

function benefit of function and all now

play24:02

in the actually we'll see one more

play24:03

coding exercise on function now this is

Rate This

5.0 / 5 (0 votes)

相关标签
Python FunctionsCoding TutorialProgramming BasicsFunction DefinitionCode ReusabilityInteractive LearningEducational ContentSoftware DevelopmentTechnical TutorialFunction Benefits
您是否需要英文摘要?