Lecture 21 & 22
Lecture 21 & 22
Wah Campus
Two-way
• if control_expression
then clause
else clause
Fall 2024
Theory Of Programming Languages 2
~~~ Dr. Khalid Iqbal Khattak ~~~
Iterative Statements
C# code
names.Add("Bob");
names.Add("Carol");
names.Add("Alice"); . . .
Console.WriteLine(name); 4.times
{puts
In Ruby
"Hey!"}
Theory Of Programming Languages
Fall 2024 4
~~~ Dr. Khalid Iqbal Khattak ~~~
Fundamentals of Sub-Programs
A subprogram is defined as a set of statements that can be reused at
multiple places in a program when convenient. This reuse results in
multiple types of savings, from memory space to coding time.
Types of Parameters:
Subprograms can be saved separately as modules and used again in
other programs (time saving, tested and debugged) Value parameters
reference parameters
• Procedures: A procedure is used to do an action output parameters
• Functions: A function is used for calculating value.
parameter arrays
can do both.
• Models called in mode,
The third array
out mode, and inout should be out mode
mode Theory Of Programming Languages
Fall 2024 18
~~~ Dr. Khalid Iqbal Khattak ~~~
Subprograms: Parameters
Formal parameters are Consider a subprogram The subprogram must add
characterized by one of that takes two arrays of int list1 to list2 and return
three distinct semantics values as parameters— the result as a revised
models: list1 and list2. version of list2
can do both.
The third array
• Models called in
mode, out mode, and
should be out
inout mode mode
Theory Of Programming Languages
Fall 2024 19
~~~ Dr. Khalid Iqbal Khattak ~~~
Subprograms: Parameters Passing
In Python, functions can take inputs, known as parameters or
arguments, to perform specific tasks based on those inputs.
def greet(name):
print(f"Hello, {name}!")
Arguments passed to a function in the exact order in which they are defined are called positional
arguments
It is the position that the arguments are placed in the brackets that is inherited, not the variable
name. Theory Of Programming Languages
Fall 2024 20
~~~ Dr. Khalid Iqbal Khattak ~~~
Subprograms: Parameters Passing
• POSITIONAL ARGUMENTS
def display_info(name, age):
print(f"My name is {name} and I am {age} years old.")
display_info("Bob", 25) # Outputs: My name is Bob and I am 25 years old.
• PASS BY REFERENCE
– pass a mutable object(list, dictionary, set) to a function and modify its
value within the function, it will affect the original object.
def modify_list(lst):
lst.append(100)
numbers = [1, 2, 3]
modify_list(numbers)
print(numbers) # Outputs: [1, 2, 3, 100]
def get_info():
name = "Alice"
age = 30
def myNum(): return name, age
num1 = (5) person_name, person_age = get_info()
myNum()
#This line both calls the functions and receives #the return value
print (num1) in to the two variables
print(person_age) # Outputs: 30
error: NameError: name 'num1' is not defined
Theory Of Programming Languages
Fall 2024 29
~~~ Dr. Khalid Iqbal Khattak ~~~
General Subprograms Characteristics
• A subprogram has a single entry point
• The caller is suspended during execution of the called
subprogram
• Control always returns to the caller when the called
subprogram's execution terminates
It is important to note that the
Main Program is suspended
during the execution of any
subprogram. Moreover, after the
completion of the subprogram,
the main program executes from
the next sequential address
present in the Program Counter.
END
_______________________________