0% found this document useful (0 votes)
9 views33 pages

Class 10 - Function - Part I

Uploaded by

sonamtashi8104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views33 pages

Class 10 - Function - Part I

Uploaded by

sonamtashi8104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

PYTHON Key stage IV Class X

དཔལ་ལྡན་འབྲུག་གཞུང་།
ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།
Department of School Education
Ministry of Education & Skills Development

Python Coding
Class X ICT Curriculum

January 2024

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Key Concept # 3

FUNCTIONS
Part I

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Concept Overview
Main concepts Sub - concepts
● Definition of function
● Purpose of function
Function ● Types of function (built-in,User-defined)
● Creating a function
● Calling a function
○ Calling function without parameter, Calling
function with parameter(s)
● Understanding function through Turtle module

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Objectives
● Define a function in Python programming.
● Recognize the benefits of using functions, such as code reuse and
modular design.
● Write the correct syntax for declaring functions in Python.
● Identify the key components of a function declaration, including the
function name, parameters, and the optional return statement.
● Differentiate between parameter and argument in a function.
● Create functions that handle multiple arguments to solve coding problem.
● Create drawings or shapes in Turtle module using functions.

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Understanding Functions
● Functions are blocks of reusable code that perform a specific task
whenever it is called.
● Functions should be given a meaningful name to make it more
readable.
● A function returns data as a result by using the return statement within
its body.
● A function is called by writing its name with the appropriate arguments.

Syntax Code
def function_name(): 1 def greet():
function body 2 print(“Hello World!”)
function_name()#function called 3 greet()#function called
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X

Importance of Function
Given below is some function importance in Python program.

Function break down a big task into smaller, more


1. Modularity
manageable parts.

Each function can do a specific job, making program


2. Readable
easier to understand and build.

Functions that have been created can be used by


3. Reusability calling them in other parts of the code as necessary,
eliminating the need to rewrite the code.
Functions make code debugging and troubleshooting
4. Troubleshooting
faster as issues to specific function can be fixed
and debugging
separately and easily.
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X

Types of Functions
● Built-in functions are part of the Python standard library.
They are available for use without the need to create
Built-in separately.
1
Functions ● Examples:
len(),print(),input(),range(),max(),min(),
sum()

● User-defined functions are created by the users based on


their needs and requirements to perform a specified task.
2 User-defined
● Example:
Functions
def greet():
print(“Kuzuzangpola”)

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Activity 1 - Built-in Functions


Write a Python program to count the Write a Python program find the sum of
number of characters in the string “Pema first five natural numbers. Hint: use a
Wangchuk”. Hint: use a built-in function. built-in function.

Code Code
1 name = “Pema Wangchuk” 1 for x in range(1,5):
2 count = len(name) 2 total + = x
3 print(“Total Char:”,count) 3 print(“Total:”, total)

Output Output
Total Char: 13 Total:10

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

ACTIVITIES

1. Understanding User-defined function

2. Demo 1 - Creating Non-Parameterized Function

3. Demo 2 - Creating Parameterized Function

4. Activity 1 - Identifying the Correct Function


User-Defined
5. Activity 2 - Understanding Parameters and
Function Arguments

6. Activity 3 - Practice Question in Function

7. Activity 4 - Practice Questions on Function

8. Demo 3 - Functions in Turtle Module

9. Activity 6 - Function in Turtle Module

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Defining User-Defined Function


● In Python a user-defined function is defined using the def keyword.
● There are two ways of defining a function.

1 2
Non-parameterized Function Parameterized Function
A function that doesn't have any Function that has one or more
parameters as an input. parameters as an input.

syntax syntax

def function_name(): def function_name(parameters):


#statements #statements
return value return value

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Examples
1 2
Non-parameterized Function Parameterized Function

1 def sub(): 1 def sub(x,y):


2 x=20 2 print(x-y)
3 y=15 3 sub(20,15)
4 print(x-y) 4
5 sub() 5

Output Output
5 5

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Terms Related to User-Defined Functions

1 def Keyword used to define a function.

2 function_name Name of the function.

3 parameters Input values that function accepts. It is optional.

Set of instructions that function will execute when


4 statements called. It must be indented.

Specifies the value that function returns when it is


5 return is called. It is optional, but if you don’t include it,
function will return None by default.

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Understanding Functions
Key word

Function Name (addition)


Code
Parameters are x and y
1 def addition(x, y):
Function Body
2 add = x + y
3 return add Return value (add) to the calling function
4 cal = addition(10, 5)
5 print(f’Addition:{cal}’) Calling function [addition(x, y)] with argument
10 and 5

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Demo 1 - Creating Non-Parameterized Function


Write a Python program to add any two numbers using non-parameterized
function.

What output will


Code be given when you
run the program?
1 def add():
2 x,y = 4,7
3 print(“Sum:”,x+y)
The output will be
empty because the
function was not
called.

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Demo 2 - Creating Parameterized Function


Create a Python program to add any two numbers. Use parameterized
function to add the numbers.

How are the two


Code programs in demo 1
& demo 2 different?
1 def add(x,y):
2 sum = x+y
3 print(sum)

Parameter x & y has


been set in the demo
2.

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Activity 1 - Identifying the correct Function

Which of the following option given below is the correct way to define a
function that will multiply two numbers?

A B C D

def mult(x,y): def mult(x,y): Display


def multempty
x,y: Def mult(x,y)
print(x*y) print(x*y) screen
print(x*y) print(x*y)

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Calling a Function
After defining a function, we need to call the function to use it. Function name
followed by parenthesis is used to call the function as given below.
Code Code
1 def add(x,y): 1 def add(a,b):
2 print(“Sum:”,x+y) 2 print(“Sum:”,a+b)
3 #Function not called 3 #Function called
4 4 add(5,9)
5 5 add(10,12)

Output Output
Sum: 14
Empty
Sum: 22
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X

Activity 3 - Multiplying Two Numbers


Write the Python program, that uses a function to multiple any two
numbers entered by the user.

Code Output

1 def multiply(x,y): Enter a value: 10


2 pro = x*y Enter a value: 2
3 print(f“{x}X{y}={pro}”) 10 X 2= 20
4 a = int(input(“Enter a value:”))
5 b = int(input(“Enter a value:”))
6 multiply(a,b)

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Activity 3 - Adding Two Numbers


Write the Python code that uses a function to add any two numbers.

Code

1 def sum(num_1, num_2):


2 add = num_1+ num_2
3 print(f“sum of {num_1} and {num_2} is {add}”)
4 sum(3,4)

Output
sum of 3 and 4 is 7

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Activity 4 - Practice Questions on Function


1. Write a parameterized function named "calculate_Area" that takes in two
parameters, length and width, and calculates the area of a rectangle. Display
the result.
2. Write a non-parameterized function, that takes input from the user and
determine whether a user-entered number is even or odd.
3. Create a Python program that use a function named “print_is_positive” that
takes a number as input and then displays whether the number is positive or
negative.
4. Write a Python program that uses a non-parameterized function to display the
first five even numbers.
5. Write a Python program that uses parameterized function to calculate the
average of all numbers upto the number entered by the user.
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Activity 2 - Understanding Parameters and
Arguments
Watch the video and explain the
differences between parameter and
argument.
1. Parameters are placeholders
that stores values.
2. Arguments are values assigned
to parameters.
3. Parameters are used when you
define a function.
4. Arguments are used when you
call a function.

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Practice Question 1 - Solution


1. Write a parameterized function named "calculate_Area" that takes in two
parameters, length and width, and calculates the area of a rectangle. Display the
result.
Code
1 def calculate_Area(l,b):
2 print(“area = ”,l * b,“unit square”)
3 length = float(input(“Enter value length:”))
4 breadth = float(input(“Enter the breadth:”))
5 calculate_Area(length, breadth)

Output
Enter value length: 2
Enter value breadth:3
area = 6.0 unit square
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X

Practice Question 2 - Solution


2. Write a non-parameterized function, that takes input from the user and
determine whether a user-entered number is even or odd.

Code Output

1 def even_odd(): Enter a number: 8


2 no=int(input("Enter a number:")) 8 is even
3 if no%2==0:
4 print(f"{no} is even")
5 else:
6 print(f"{no} is odd")
7 even_odd()

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Practice Question 3 - Solution


3. Create a Python program that use a function named “print_is_positive” that
takes a number as input and then displays whether the number is positive or
negative.

Code Output

1 def print_is_positive(n): Enter a number: 9


2 if n > 0: positive
3 print("positive")
4 else:
5 print("negative")
6 num=int(input("Enter a number:"))
7 print_is_positive(num)

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Practice Question 4 - Solution


4. Write a Python program that uses a non-parameterized function to
display the first five even numbers.

Code Output

1 def first_five_even_number(): 2
2 for num in range(2,11): 4
3 if num%2==0: 6
4 print(num) 8
5 first_five_number() 10

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Practice Question 5 - Solution


5. Write a Python program that Code
uses parameterized function
to calculate the average of 1 def average(n):
all the numbers upto the 2 i = 1
number entered by the user. 3 total = 0
4 if n==0:
Output 5 print(“Invalid input!”)
6 else:
Enter number: 10 7 while i <= n:
8 total = total+i
Average:5.5 9 i += 1
10 average = total/n
11 print(f‘Average:{average}’)
12 num = int(input(“Enter number:”))
13 average(num)

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Demo 3 - Drawing Circles


Write a Python program using Turtle module to draw two circles with a radius
50. Use function to perform the task.

Code
1 from turtle import *
2 title(“Drawing circles”)
3 def draw_circle():
4 pendown()
5 circle(50)
6 penup()
7 draw_circle()
8 goto(50,-100)
9 draw_circle()

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Demo 4 - Drawing Squares


Write a Python program to draw two Code
squares with side length of 200 pixels
in Turtle module. Use a function. 1 from turtle import *
2 title(“Drawing squares”)
3 goto(-100,0)
4 def draw_square():
5 penup()
6 for i in range(4):
7 fd(100)
8 lt(90)
9 pendown()
10 draw_sqaure()
11 goto(0,-100)
12 draw_square()
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X

Activity 5 - Drawing a Hexagon


Write a Python program to draw hexagon shape with sides of 100 pixels using
the Turtle module.

Code
1 from turtle import *
2 title(“Drawing Hexagon”)
3 def draw_hexagon():
4 for i in range(6):
5 forward(100)
6 left(60)
7 draw_hexagon()

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Activity 6 - Drawing a Composite Shape


Write a Python program to create Code
the following output.
1 from turtle import *
2 title('Drawing Shape')
3 def Shape(radius):
4 for i in range(4):
5 forward(100)
6 left(90)
7 color("blue")
8 forward(radius)
9 begin_fill()
10 circle(radius)
11 end_fill()
12 Shape(50)
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X

Activity 7 - Drawing Olympic Logo


Write a Python program to draw Code
the olympic logo using Turtle
1 from turtle import *
module.
2 pensize(10)
3 def olympic_logo(x,y,color):
4 penup()
5 goto(x,y)
6 pendown()
7 pencolor(color)
8 circle(50)
9 olympic_logo(0,140,"blue")
10 olympic_logo(70,100,"yellow")
11 olympic_logo(140,140,"black")
12 olympic_logo(210,100,"green")
13 olympic_logo(280,140,"red")

January 2024 © ICT Curriculum, MoESD


PYTHON Key stage IV Class X

Key
Points
● Function is a block of reusable code that performs a specific task.
● Using functions in programs help in code readability, reusability and
debugging.
● Functions are defined using the def keyword followed by function name
and parenthesis.
● There are two ways of defining a function - parameterized and
non-parameterized.
● Arguments are values assigned to parameters and they are used when
calling a function.
● Functions can be used to solve different types of problems including
drawing shapes using Turtle module.
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X

བཀྲིན་ཆེ།
THANK YOU

January 2024 © ICT Curriculum, MoESD

You might also like