0% found this document useful (0 votes)
83 views3 pages

Subprograms (Functions) in Python: Grade 09 - Topic 02 - Programming and Development Note - Part Iv

This document discusses subprograms (functions) in Python. It explains that there are two types of subprograms: built-in subprograms that are predefined, and user-defined subprograms that are created by the programmer. Built-in subprograms include functions like print(), int(), and range(). The document also describes how to create a user-defined subprogram to add two numbers, including using the def keyword, defining parameters, optional docstrings, return statements, and how local variables work only within the subprogram.

Uploaded by

Shehan Morawaka
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)
83 views3 pages

Subprograms (Functions) in Python: Grade 09 - Topic 02 - Programming and Development Note - Part Iv

This document discusses subprograms (functions) in Python. It explains that there are two types of subprograms: built-in subprograms that are predefined, and user-defined subprograms that are created by the programmer. Built-in subprograms include functions like print(), int(), and range(). The document also describes how to create a user-defined subprogram to add two numbers, including using the def keyword, defining parameters, optional docstrings, return statements, and how local variables work only within the subprogram.

Uploaded by

Shehan Morawaka
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/ 3

Grade 09 - Topic 02 – Programming and Development

NOTE - PART IV

Subprograms (functions) in Python


❖ Subprograms are individual modules of a program that have only one duty assigned to it to perform.
❖ It is a grouped set of program statements written to achieve a specific task inside a program.
❖ Also known as functions.
❖ We can have one or more subprograms used by a single Python program.
❖ There are 2 types:
1. Built-in subprograms.
2. User-defined subprograms.

Built-in subprograms
❖ Built-in subprograms are the subprograms that are given for us to use in our programs.
❖ They are developed by other programmers.
❖ Examples of built-in subprograms we have already used so far:
o print() function : Displays a message as a string.
o input() function : Takes user input as a string.
o int() function : Converts a string value into an integer value.
o float() function : Converts a string value into a float value.
o str() function : Converts a value into a string value.
o range() function : Returns a sequence of numbers starting with 0 by default.
❖ Some subprograms need one or more values written inside their parenthesis to perform the
operation they supposed to perform. Those values are known as argument(s).
❖ Examples:
o print(“Hi”) : To display the string Hi.
o int(“105”) : To convert string 105 to its integer value.
o range(5) : To return 5 numbers starting from 0.

Sunday, October 23, 2022 Page 1 of 3


Grade 09 - Topic 02 – Programming and Development
NOTE - PART IV

User-defined subprograms
❖ We can create our own subprograms in Python. Such subprograms are known as user-defined
subprograms.
❖ We can create and use one or more user-define subprograms in a single Python program.
❖ For an example, let us create our own subprogram to calculate and return the total of two integers if
we receive the two integers as arguments.

❖ Parts of a user-defined subprogram:


o def : All user-defined subprograms must start with def keyword.
o Name : Each user-defined subprogram must have a unique name. You cannot use the same
name for more than one subprograms.
o Parameters : One or more variables (also known as local variables in a subprogram) to store
the arguments sent to the subprogram.
o Docstring (Document String) : An optional small description as a string to describe the
subprogram.
o Python program statement(s) : One or more Python statements to perform and complete the
operation.
o Return statement : Return statement returns the final result of the subprogram back to the
place that called the subprogram by sending arguments. The value returned by this statement
is known as the return value.
▪ Some subprograms do not need to return a result at the end. In such cases, return
statement only include the keyword return.

Sunday, October 23, 2022 Page 2 of 3


Grade 09 - Topic 02 – Programming and Development
NOTE - PART IV

❖ In a Python program, all variables that are created outside subprograms are considered as global
variables.
❖ All variables that are created within a subprogram (including the parameter variables) are considered
as local variables.
❖ So, we have three local variables created in the above example.
o X : Parameter 01.
o Y : Parameter 02.
o Sum : Variable to store the result of the addition.
❖ Local variables are only visible within the subprogram where the variable is created.
o That means in our example, we cannot use or access X, Y or Sum outside the subprogram
addNumbers. Those variables are only visible within the subprogram addNumbers.
❖ To use the subprogram we created, we have to add some more code at the end like shown below:

❖ Explanation of the above code:


1. Program starts to run from Line 05. addNumbers(10, 20) will send arguments 10 and 20 (2
arguments) to addNumbers subprogram.
2. Then in Line 01, addNumbers subprogram takes the arguments sent from Line 05 and stores
them inside X and Y respectively (10 will be stored inside X and 20 will be stored inside Y).
3. In Line 03, values stored inside X and Y will be added together and the result is stored inside
Sum variable. (So, after this line, Sum variable stores 30 inside it.)
4. In Line 04, the value currently stored inside Sum variable will be returned again to Line 05.
5. Again in Line 05, the value returned by Line 04 is stored inside the additionResult variable.

Sunday, October 23, 2022 Page 3 of 3

You might also like