Top Banner
L i t t l e C o d e r P r o g r a m by
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Baabtra.com little coder   chapter - 7

Little Coder Program

by

Page 2: Baabtra.com little coder   chapter - 7

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

Page 3: Baabtra.com little coder   chapter - 7

FunctionsRe-using your code

Chapter 7

Page 4: Baabtra.com little coder   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.

Page 5: Baabtra.com little coder   chapter - 7

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)

Page 6: Baabtra.com little coder   chapter - 7

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

Page 7: Baabtra.com little coder   chapter - 7

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

Page 8: Baabtra.com little coder   chapter - 7

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.

Page 9: Baabtra.com little coder   chapter - 7

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()

Page 10: Baabtra.com little coder   chapter - 7

Calling a function

>>> def testfunc(myname):

print('hello %s' % myname)

>>> testfunc('Mary')

Function body or definition

Calling the function

Page 11: Baabtra.com little coder   chapter - 7

Calling a function

>>> def testfunc(myname):

print('hello %s' % myname)

>>> testfunc('Mary')

Function body or definition

Calling the function

Output

hello Mary

Page 12: Baabtra.com little coder   chapter - 7

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

Page 13: Baabtra.com little coder   chapter - 7

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

Page 14: Baabtra.com little coder   chapter - 7

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

Page 15: Baabtra.com little coder   chapter - 7

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

Page 16: Baabtra.com little coder   chapter - 7

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

Page 17: Baabtra.com little coder   chapter - 7

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

Page 18: Baabtra.com little coder   chapter - 7

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

Page 19: Baabtra.com little coder   chapter - 7

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

Page 20: Baabtra.com little coder   chapter - 7

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

Page 21: Baabtra.com little coder   chapter - 7

Example Returning a value

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

return (science+english+maths)/3

>>>a=40

print(a) Output40

Page 22: Baabtra.com little coder   chapter - 7

Exercise !

Page 23: Baabtra.com little coder   chapter - 7

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

Page 24: Baabtra.com little coder   chapter - 7

End of Chapter 7

Page 25: Baabtra.com little coder   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

Page 26: Baabtra.com little coder   chapter - 7

Course Exercise !

Page 27: Baabtra.com little coder   chapter - 7

Write python program to draw below shapes using loops

Page 28: Baabtra.com little coder   chapter - 7

End of little Coder

Hope you are ready to do the teen Coder program

with