Baabtra.com little coder chapter - 7

28
L i t t l e C o d e r P r o g r a m by

Transcript of Baabtra.com little coder chapter - 7

Little Coder Program

by

What you’ve learned?

What are loops and how it reduce the effort of reducing instructions

What is for loop and how to iterate with it

What is range() function and how to iterate through lists

How to iterate with while loop

FunctionsRe-using your code

Chapter 7

Functions

• A function is a group of statements that together perform a task

• Functions are one way to reuse code—you can use functions in

your programs again and again.

Parts of a Function

• A function has three parts: a name, parameters, and a body.

Here’s an example of a simple function:

>>> def testfunc(myname):

print('hello %s' % myname)

Parts of a Function

• A function has three parts: a name, parameters, and a body.

Here’s an example of a simple function:

>>> def testfunc(myname):

print('hello %s' % myname)Def is a python keyword

provided for programmers to create a

python function

Parts of a Function

• A function has three parts: a name, parameters, and a body.

Here’s an example of a simple function:

>>> def testfunc(myname):

print('hello %s' % myname)

Testfunc is the name of the function. We can give any name for the

function as per our convenience

Parts of a Function

• A function has three parts: a name, parameters, and a body.

Here’s an example of a simple function:

>>> def testfunc(myname):

print('hello %s' % myname)

Myname is called as the parameter of the function. A parameter is a variable that exists only while a function is being used.

Parts of a Function

• A function has three parts: a name, parameters, and a body.

Here’s an example of a simple function:

>>> def testfunc(myname):

print('hello %s' % myname)

This space indicates that we are going to start a block. Which is obviously the body of the function. And in our body there is only one statement which is print()

Calling a function

>>> def testfunc(myname):

print('hello %s' % myname)

>>> testfunc('Mary')

Function body or definition

Calling the function

Calling a function

>>> def testfunc(myname):

print('hello %s' % myname)

>>> testfunc('Mary')

Function body or definition

Calling the function

Output

hello Mary

Calling a function

>>> def testfunc(myname):

print('hello %s' % myname)

>>> testfunc('Mary')

The value Mary will be assigned to the variable myname during the function call

Output

hello Mary

Returning a value

• A function is often used to return a value, using a return

statement. For example, you could write a function to calculate

your average mark

>>>def averageMark(science,english,maths)

return (science+english+maths)/3

>>>a=averageMark(35,35,50)

>>>print(a)

40

Example Returning a value

>>> def averageMark(science,english,maths)

return (science+english+maths)/3

>>> a=averageMark(35,35,50)

print(a)

Function body or definition

Calling the function

Example Returning a value

>>> def averageMark(science,english,maths)

return (science+english+maths)/3

>>>a=averageMark(35,35,50)

print(a)

If you call the function averageMark() it will calculate and return the average

Calling the function

Example Returning a value

>>> def averageMark(science,english,maths)

return (science+english+maths)/3

>>>a=averageMark(35,35,50)

print(a)

This is where we call the function averageMark(). We pass three parameters 35,35,50 during function call

Example Returning a value

>>> def averageMark(science,english,maths)

return (science+english+maths)/3

>>>a=averageMark(35,35,50)

print(a)

This is where we call the function averageMark(). We call it with three parameters 35,35,50

Example Returning a value

>>> def averageMark(science,english,maths)

return (science+english+maths)/3

>>>a = averageMark(35,35,50)

print(a)

It calculate and returns the average value. Ie 40

Example Returning a value

>>> def averageMark(science,english,maths)

return (science+english+maths)/3

>>>a = averageMark(35,35,50)

print(a)

So this selected potion will be replaced with the returned value. Ie 40

Example Returning a value

>>> def averageMark(science,english,maths)

return (science+english+maths)/3

>>>a=40

print(a)

So variable a will be having a value 40

Example Returning a value

>>> def averageMark(science,english,maths)

return (science+english+maths)/3

>>>a=40

print(a) Output40

Exercise !

Exercise !

• Create a function named “MyDetails()” , calling which will print all the

details about you

• Create a function to calculate the square of any number passed into it

• Crate a function that will return the square of any number passed in to it

End of Chapter 7

What you’ve learned?

What is function ? And how it will help you to reuse your code

What is parameters? How to pass parameter in to a function

How to return a value from a function

Course Exercise !

Write python program to draw below shapes using loops

End of little Coder

Hope you are ready to do the teen Coder program

with