0% found this document useful (0 votes)
5 views

Program instructions execution, variables and their types, functions

The document outlines the process of program execution, detailing the differences between compiled and interpreted languages, as well as execution flow and the CPU's execution cycle. It explains variables, their declaration, data types, and type conversion, along with the concept of functions, including their definition, parameters, arguments, return values, and variable scope. The conclusion emphasizes the sequential nature of program instructions, the role of variables in data storage, and the importance of functions for code organization and reusability.

Uploaded by

Elizabeth Gomez
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)
5 views

Program instructions execution, variables and their types, functions

The document outlines the process of program execution, detailing the differences between compiled and interpreted languages, as well as execution flow and the CPU's execution cycle. It explains variables, their declaration, data types, and type conversion, along with the concept of functions, including their definition, parameters, arguments, return values, and variable scope. The conclusion emphasizes the sequential nature of program instructions, the role of variables in data storage, and the importance of functions for code organization and reusability.

Uploaded by

Elizabeth Gomez
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/ 6

Program Instructions Execution, Variables, and Functions

---

1. Program Instructions Execution

Program execution is the process where a computer follows a sequence of instructions written
in a programming language. These instructions are executed step by step by the CPU (Central
Processing Unit) according to the program logic. The execution process typically involves:

1.1 Compilation vs. Interpretation

Compiled Languages (C, C++, Java): The entire code is translated into machine code before
execution. The CPU then executes this machine code.

Interpreted Languages (Python, JavaScript): The code is executed line by line by an interpreter.

1.2 Execution Flow

Sequential Execution: The instructions are executed one after another in the order they appear.

Conditional Execution: Based on conditions (if-else statements), the program decides which
instructions to execute.

Loop Execution: Repeats a set of instructions multiple times (for, while loops).

Function Execution: Executes specific blocks of reusable code when called.

1.3 Execution Cycle (Fetch-Decode-Execute)

The CPU follows these steps to execute an instruction:

1. Fetch: Retrieves the next instruction from memory.

2. Decode: Translates the instruction into machine code.

3. Execute: Performs the operation specified (e.g., arithmetic calculation, memory access).
---

2. Variables and Their Types

A variable is a named storage location in memory used to hold data that can change during
program execution.

2.1 Declaring and Assigning Variables

In most programming languages, variables are declared before use.


Example in Python:

x = 10 # Integer variable
name = "Alice" # String variable

2.2 Data Types

Data types define the kind of values a variable can store. Common types include:
2.3 Type Conversion

Variables can be converted between types using built-in functions.


Example:

x = 10
y = float(x) # Converts integer to float (10.0)

---

3. Functions

A function is a reusable block of code that performs a specific task. Functions help in code
modularity and reusability.

3.1 Defining and Calling Functions


Example in Python:

def greet(name):
return f"Hello, {name}!"

print(greet("Alice"))

3.2 Function Parameters and Arguments

Parameters: Variables defined in the function signature.

Arguments: Values passed to the function when called.


Example:

def add(a, b): # Parameters


return a + b

result = add(5, 3) # Arguments

3.3 Types of Functions


3.4 Function Return Values

A function can return values using the return statement.


Example:

def square(num):
return num * num

result = square(4) # result = 16

3.5 Scope of Variables

Local Variables: Exist only within a function.

Global Variables: Declared outside functions and can be accessed globally.


Example:
global_var = "I am global"

def my_function():
local_var = "I am local"
print(local_var)

print(global_var) # Accessible
# print(local_var) # Error: Not accessible outside function

---

Conclusion

Program instructions execute in sequence unless control structures (loops, conditions) alter the
flow.

Variables store different data types and can be converted as needed.

Functions help organize code and allow reusability with parameters and return values.

You might also like