MFA CSE FONModularity_and_Functions.pptx

SyedaNooreen 2 views 10 slides Mar 10, 2025
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

The topic is structured as follows:
 Abstraction and Modularity.
 Decomposition & refinement.
 Functions in Python, including:
o Parameters/ Arguments.
o Return values.
o Calling functions.
o Defining functions.
 Built in functions and libraries.


Slide Content

Modularity and Functions in Python IGCSE Foundation Year A Two-Hour Interactive Session

Introduction to Modularity • Modularity helps in structuring complex programs. • It promotes code reusability and maintainability. • Includes Abstraction, Decomposition, and Refinement.

Decomposition & Refinement • Breaking a large problem into smaller, manageable parts. • Each part (function) solves a specific task. • Example: A program for an online calculator.

Functions in Python • A function is a reusable block of code. • Defined using the 'def' keyword. • Helps organize and structure code.

Defining a Function - Example def greet(): print('Hello, welcome to Python!') greet() # Calling the function

Parameters and Arguments • Parameters are variables inside the function definition. • Arguments are the actual values passed when calling the function. Example: def add(a, b): return a + b result = add(5, 3) # Arguments are 5 and 3

Return Values in Functions • Functions can return values using the 'return' statement. Example: def square(num): return num * num print(square(4)) # Output: 16

Built-in Functions & Libraries • Built-in functions: print(), len(), input(), type(), etc. • Libraries: Importing external functionalities. Example: import math print(math.sqrt(25)) # Output: 5.0

Local vs Global Variables • Local variables exist inside a function. • Global variables exist throughout the program. Example: global_var = 10 def example(): local_var = 5 print(local_var) # Accessible here print(global_var) # Accessible everywhere

Hands-on Exercises 1. Write a function to calculate the area of a rectangle. 2. Create a function that returns the maximum of three numbers. 3. Use the math library to calculate the square root of a user-inputted number. 4. Experiment with local and global variables.
Tags