0% found this document useful (0 votes)
2 views15 pages

Chapter 1

The document is a course outline for Computer Science 2 at Morsli Abdellah University Center, focusing on functions and procedures in programming. It covers topics such as algorithmic thinking, the definition and use of procedures and functions, parameters, and the differences between local and global variables. The course emphasizes the importance of code organization, reusability, and modularity in software development.

Uploaded by

wahab.ayoub1234
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)
2 views15 pages

Chapter 1

The document is a course outline for Computer Science 2 at Morsli Abdellah University Center, focusing on functions and procedures in programming. It covers topics such as algorithmic thinking, the definition and use of procedures and functions, parameters, and the differences between local and global variables. The course emphasizes the importance of code organization, reusability, and modularity in software development.

Uploaded by

wahab.ayoub1234
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/ 15

Morsli Abdellah University Center in Tipaza

Institute of Sciences
Department of science and technology (ST) – 1st Year (LMD)
Course : Computer Science 2 ( S2)

Chapter 1: Functions and Procedures

Table of Contents
1. Introduction ........................................................................................................................................... 3

2. Algorithmic thinking ............................................................................................................................. 4

2.1. Designing algorithms ................................................................................................................. 4

2.2. Combining solutions .................................................................................................................. 4

2.3. Iterative refinement .................................................................................................................... 4

3. Procedures ............................................................................................................................................. 4

3.1. What is a procedure? .................................................................................................................. 4

3.2. Use Cases ................................................................................................................................... 5

3.3. Execution Flow .......................................................................................................................... 5

3.4. Side Effects ................................................................................................................................ 5

3.5. How to make (define) a procedure in python? ........................................................................... 5

3.5.1. Procedure syntax ................................................................................................................ 5

3.5.2. Calling Procedures ............................................................................................................. 6

4. Functions ............................................................................................................................................... 7

4.1. What is a function? .................................................................................................................... 7

4.2. Function syntax .......................................................................................................................... 7

4.3. Calling a Function ...................................................................................................................... 8

4.4. Characteristics of Functions ....................................................................................................... 8

4.5. Documentation String (Docstring) ............................................................................................. 8

5. Parameters (arguments) in procedures and functions ............................................................................. 9

5.1. Parameters in Procedure /Function Definition ........................................................................... 9

Dr. Mansouri 2024/2025


5.2. Arguments in Procedure/function Call ....................................................................................... 9

5.2.1. Positional Arguments ......................................................................................................... 9

5.2.2. Keyword Arguments .......................................................................................................... 9

5.2.3. Default Parameter Values ................................................................................................ 10

5.3. Local variables Vs. Global Variables ................................................................................... 10

5.3.1. Local variables ................................................................................................................. 10

5.3.2. Global Variables .............................................................................................................. 10

5.4. Examples ................................................................................................................................. 11

5.4.1. About Procedures ............................................................................................................. 11

5.4.2. About Functions ............................................................................................................... 12

6. Procedures Vs. functions ..................................................................................................................... 15

7. Practices........................................................................................................................................... 15

Dr. Mansouri 2024/2025


1. Introduction
In the world of programming, procedures and functions serve as fundamental building blocks for
organizing and executing code efficiently, playing a crucial role for several reasons:
Code Organization:
- Procedures and functions provide a structured approach to organizing code. By breaking down
a program into smaller, manageable units, developers can better understand and maintain the
codebase.
- Dividing the code into logical sections makes it easier to navigate, debug, and modify,
especially in larger projects with multiple contributors.
Reusability:
- Procedures and functions promote code reusability by encapsulating specific tasks or
functionalities. Once defined, a procedure or function can be invoked (called) multiple times
from different parts of the program.
- Instead of duplicating code for similar tasks, developers can write reusable
procedures/functions, reducing redundancy and promoting efficiency.
- Reusable code components save time and effort, as developers can leverage existing solutions
rather than reinventing the wheel.
Modularity:
- Procedures and functions facilitate modularity, the principle of dividing a program into
independent, interchangeable components. Each procedure or function represents a modular
unit with well-defined inputs, outputs, and functionality.
- Modular code is easier to understand, test, and maintain. Developers can focus on
implementing and troubleshooting individual modules without worrying about the intricacies
of the entire program.
- Modularity fosters collaboration among team members, as developers can work on different
modules simultaneously without interfering with each other's code.
Scalability and Maintainability:
- By promoting code organization, reusability, and modularity, procedures and functions
contribute to the scalability and maintainability of software projects.
- As projects grow in size and complexity, well-structured code with reusable components
becomes essential for managing complexity, adapting to changing requirements, and
facilitating future enhancements

Dr. Mansouri 2024/2025


2. Algorithmic thinking
is A problem-solving approach that involves systematically breaking down complex problems into
smaller, more manageable tasks. Here's A summary of the key points:

✓ Problem understanding: begin by thoroughly understanding the problem statement, including


inputs, outputs, and constraints.

✓ Decomposition: break the problem down into smaller sub-problems or tasks, making it easier
to tackle each component individually.

✓ Identifying patterns: look for recurring patterns or similarities among the sub-problems, which
can help in developing reusable solutions.

2.1. Designing algorithms


develop algorithms (programs) to solve each sub-problem efficiently, specifying inputs,
processing steps, and outputs.
2.2. Combining solutions
integrate or combine algorithms (programs) to solve the larger problem, coordinating their
execution to achieve the desired outcome.
2.3. Iterative refinement
continuously refine solutions through testing, debugging, and performance analysis,
optimizing algorithms for efficiency and accuracy. Algorithmic thinking enables effective
problem-solving by providing a structured approach to breaking down complex problems,
identifying patterns, designing algorithms, and iteratively refining solutions. This approach
is fundamental in various domains, from computer science and engineering to mathematics
and beyond.

3. Procedures
3.1. What is a procedure?
- A procedure is a named block of code that performs a specific task or a set of tasks. It is a
sequence of instructions that can be executed one after another. Procedures help in breaking
down complex problems into smaller, manageable tasks, making the code more organized and
easier to understand.
- procedures simply execute their code and may perform actions or tasks and do not return any
value.

Dr. Mansouri 2024/2025


3.2. Use Cases
- Procedures are commonly used for tasks like input/output operations, data processing, event
handling, and system interactions.
- They encapsulate a sequence of steps to be executed, providing a reusable and modular way to
organize code.
- Procedures are typically used when you need to perform a series of actions or tasks without
needing to compute and return a result.
- They are often employed for tasks like printing output, modifying data, or interacting with
external systems.
3.3. Execution Flow
- When a procedure is called, Python executes its code sequentially, performing any actions or
tasks defined within the procedure.
- Once the procedure completes its execution, control returns to the caller without any returned
value.
3.4. Side Effects
- Procedures may have side effects, meaning they can modify data or produce output without
returning a value.
- This can include actions like printing to the console, modifying mutable data structures, or
interacting with external resources.
3.5. How to make (define) a procedure in python?
3.5.1. Procedure syntax
In Python, a procedure is defined using the `def` keyword followed by the name of the
procedure. The syntax for defining a procedure is as follows:

• Procedure Name:
- The procedure name should follow the same rules as variable names in Python.
Dr. Mansouri 2024/2025
- It should be descriptive and indicative of the task the procedure performs.
• Parentheses:
- After the procedure name, a pair of parentheses `()` is used.
- These parentheses may or may not contain parameters (placeholders for data passed into the
procedure).
• Colon:
- Following the parentheses, a colon `:` is used to indicate the start of the procedure's body. -
The colon is a required part of the syntax and signifies the beginning of an indented code block.
• Procedure body:
- The procedure body consists of one or more indented lines of code that define the actions or
tasks the procedure performs.
- All code within the procedure body must be indented to the same level to denote that it belongs
to the procedure.
• Indentation:
- Python uses indentation to define the scope of code blocks, including procedure bodies.
- All code within the procedure must be indented consistently, typically using four spaces for
each level of indentation.
• Whitespace:
- Whitespace (spaces or tabs) before the `def` keyword or after the colon is optional but
recommended for readability.
- Consistent use of whitespace helps improve code readability and maintainability
3.5.2. Calling Procedures
Usage:
- Understanding how to call procedures correctly is crucial for leveraging their functionality
within a program.
- Proper usage ensures that procedures receive the necessary input data to perform their tasks
effectively.
Syntax:
- To call a procedure in Python, you use its name followed by parentheses containing any
required arguments.
- If the procedure has no parameters, you still need to include empty parentheses `()`.

Dr. Mansouri 2024/2025


4. Functions
4.1. What is a function?
Functions are a type of procedure that can accept input, perform computations, and return
results. They are essentially reusable code blocks that can be invoked (called) multiple times
within a program with different inputs, producing consistent outputs.
Examples of Functions:
- Buttons on a remote control serve as functions. Each button performs a specific action, such as
changing channels, adjusting volume, or turning the device on/off.
- Applications on smartphones offer various functions tailored to specific tasks. For example, a
weather app provides weather forecasts, a calculator app performs mathematical calculations, and
a messaging app allows sending and receiving messages.
4.2. Function syntax
In Python, a function is defined using the `def` keyword followed by the name of the function. The
syntax for defining a function is as follows:

A function is a named block of code that performs a specific task and usually returns a value.
Functions are primarily used for computation and produce an output based on the input
parameters.
Function Name:

Following the `def` keyword, you specify the name of the function.

Function names should be descriptive and follow Python's identifier naming rules (e.g., no
spaces, start with a letter or underscore, etc)

Parameters (optional):
- Inside the parentheses `()`, you can specify parameters that the function accepts.
- Parameters are variables that you define in the function signature to receive input values.
Dr. Mansouri 2024/2025
- Parameters are separated by commas.
Colon `:`:
- After specifying the function name and parameters (if any), you end the function declaration
line with a colon `:`.
- The colon indicates the beginning of the function body.
- Indentation and Whitespace are also the same as procedures
4.3. Calling a Function
Once a function is defined, you can call it from anywhere in your code (after the declaration). To
call a function, simply use its name followed by parentheses `()`, optionally passing arguments if
the function expects them. If there's no return statement, the function returns `None` by default
4.4. Characteristics of Functions
- Return Value: Functions typically return a value.
- Pure Functions: Functions are often pure, meaning they produce the same output for the same
input and do not have side effects.
- Usage: Functions are used when you need to compute a value based on input parameters.
4.5. Documentation String (Docstring)
Is a string literal that occurs as the first statement in a module, function, class, or method definition
in Python. It is used to provide documentation about the purpose and usage of the code element it
precedes. Docstrings are optional but highly recommended as they serve as valuable documentation
for your code.

Example:

Dr. Mansouri 2024/2025


5. Parameters (arguments) in procedures and functions
5.1. Parameters in Procedure /Function Definition
• Parameters are placeholders for data that a procedure/functions expects to receive when
it is called.
• They are defined in the procedure/function header within the parentheses following the
procedure name.
• Parameters allow procedures /functions to accept input data, making them more
versatile and reusable.
• When defining a procedure/function, parameter names are used to represent the data
that the procedure will receive.
• Parameters act as local variables within the procedure/function body, taking on the
values provided when the procedure is called.
5.2. Arguments in Procedure/function Call
- Arguments are the actual values or variables passed to a procedure /function when it is
called.
- They are provided within the parentheses following the procedure/function name in the
procedure call.
- If a procedure/function has no parameters, you still need to include empty parentheses
when calling it to signify that it's a procedure call.
- Arguments supply the input data required by the procedure/function, which can be
specific values, variables, or expressions.
5.2.1. Positional Arguments
- When calling a procedure/function with positional arguments, you provide values in the
same order as the parameters are defined in the procedure's /function’s header.
- Each argument is separated by a comma.
5.2.2. Keyword Arguments
- Keyword arguments are arguments passed to a procedure/a function with the parameter names
explicitly specified.
- They allow for more flexibility in calling procedures/functions, as they can be provided in any
order.

Dr. Mansouri 2024/2025


5.2.3. Default Parameter Values
- Parameters in Python procedures/functions can have default values specified, which are used
if no argument is provided for that parameter.
- Default parameter values are specified in the procedure/function header using the assignment
operator (`=`).
5.3. Local variables Vs. Global Variables
Understanding local and global variables in procedures/functions is essential for writing modular
and maintainable code. Proper usage of local variables keeps the procedure's /function’s logic
encapsulated, while judicious use of global variables allows for sharing data across different parts
of the program.
5.3.1. Local variables
- Local variables in procedures/functions are variables that are defined within the procedure's
scope.
- They are accessible only within the procedure/function where they are defined and cannot be
accessed from outside the procedure/the function.
- Local variables have a local scope, limited to the block of code where they are defined,
typically inside a procedure/a function.
- They are created when the procedure/function is called and are destroyed when the procedure/
the function exits.
- Similar to functions, local variables in procedures can "shadow" variables with the same name
in outer scopes.
- This allows procedures/functions to use variables with the same names as variables in the
global scope without affecting the global variables.
5.3.2. Global Variables
- Global variables in procedures/functions are variables defined outside of any
procedure's/function’s scope or declared with the `global` keyword inside a procedure/function.
- They can be accessed and modified from anywhere in the code, including inside
procedures/functions.
- Global variables have a global scope, accessible from anywhere in the code, including inside
procedures/functions using `global` keyword .
- Inside a procedure, the `global` keyword allows you to modify a global variable from within
the procedure's scope.

Dr. Mansouri 2024/2025


- Without the `global` keyword, assigning a value to a variable inside a procedure creates a new
local variable with the same name.
- They are created when the program starts and persist until the program terminates.
5.4. Examples
5.4.1. About Procedures
1-

2-

3-

4-

Dr. Mansouri 2024/2025


5-
5.4.2. About Functions
1.

2.

Dr. Mansouri 2024/2025


3.

4.

Dr. Mansouri 2024/2025


5.

6.

Dr. Mansouri 2024/2025


6. Procedures Vs. functions
• Procedures and functions are blocks of code that perform specific tasks or computations.
• Procedures are sequences of instructions that can accept inputs, known as parameters
or arguments, to customize their behavior. However, procedures typically do not return
values.
• Functions accept also the inputs (parameters), process them, and return results.

7. Practices
• Write a procedure that takes a list as input and prints a new list with duplicates removed.
• Write a procedure that takes two strings as input and prints whether they are anagrams of each
other or not.
• Write a Python function named check_prime_number that takes a number as input and returns
a Boolean value indicating if the number is prime or not.
• Write a Python function named find_common_elements that takes two lists as input and returns
a list containing the common elements between the two lists.

Dr. Mansouri 2024/2025

You might also like