What is a Procedure?
Key Points
A procedure is a reusable group of instructions
Also called functions or methods
Lets you avoid rewriting the same code
JavaScript
// A procedure (function) to display a message
function showMessage() {
console.log("Welcome to the program!");
}
In JavaScript, a procedure is called a function.
- Use the function keyword to define a procedure.
- Write your instructions inside curly braces {}.
This function displays a welcome message.
Python
# A procedure (function) to display a message
def show_message():
print("Welcome to the program!")
In Python, a procedure is called a function.
- Use the def keyword to define a procedure.
- Write your instructions inside an indented block.
This function displays a welcome message.
Parts of a Procedure
Key Points
Procedures can have parameters (inputs)
Parameters are variables used inside the procedure
You can define procedures with or without parameters
JavaScript
// Function with parameters
function multiplyNumbers(a, b) {
console.log(a * b);
}
In JavaScript, parameters go inside the parentheses.
- a and b are parameters.
- The function multiplies them and prints the result.
You can use any number of parameters.
Python
# Function with parameters
def multiply_numbers(a, b):
print(a * b)
In Python, parameters go inside the parentheses.
- a and b are parameters.
- The function multiplies them and prints the result.
You can use any number of parameters.
Developing vs. Calling a Procedure
Key Points
Developing a procedure means writing the code for what it does.
Calling a procedure means using it in your program.
You must develop (define) a procedure before you can call it.
JavaScript
// Developing (defining) the procedure
function double(num) {
console.log(num * 2);
}
// Calling the procedure
double(5);
Developing a procedure is when you write the function itself, including its name, parameters, and instructions.
- Example: function double(num) { console.log(num * 2); }
Calling a procedure is when you use its name to make it run.
Python
# Developing (defining) the procedure
def double(num):
print(num * 2)
# Calling the procedure
double(5)
Developing a procedure is when you write the function, including its name, parameters, and instructions.
- Example: def double(num): print(num * 2)
Calling a procedure is when you use its name to make it run.
Calling a Procedure
Key Points
Call a procedure by its name
Provide arguments if it has parameters
Arguments are the actual values you use
JavaScript
// Call the function with arguments
multiplyNumbers(3, 4); // Output: 12
multiplyNumbers(7, 2); // Output: 14
When you call a procedure, you use its name and provide arguments.
- multiplyNumbers(3, 4) calls the function with 3 and 4.
- The function prints 12.
Arguments are the actual values you use when calling the procedure.
Python
# Call the function with arguments
multiply_numbers(3, 4) # Output: 12
multiply_numbers(7, 2) # Output: 14
When you call a procedure, you use its name and provide arguments.
- multiply_numbers(3, 4) calls the function with 3 and 4.
- The function prints 12.
Arguments are the actual values you use when calling the procedure.
Using Return Statements
Key Points
Use return to send a value back
Return ends the procedure immediately
Returned values can be used elsewhere in your code
JavaScript
function addNumbers(a, b) {
let sum = a + b;
return sum;
}
let result = addNumbers(5, 7);
console.log(result); // Output: 12
In JavaScript, return sends a value back.
- The function adds two numbers and returns the sum.
- You can store the returned value in a variable.
return ends the function and gives back the result.
Python
def add_numbers(a, b):
sum_value = a + b
return sum_value
answer = add_numbers(5, 7)
print(answer) # Output: 12
In Python, return sends a value back.
- The function adds two numbers and returns the sum.
- You can store the returned value in a variable.
return ends the function and gives back the result.
Try It Yourself: Call a Procedure
Key Points
Practice calling a procedure with an argument
See how the procedure uses your input
Try It Yourself (JavaScript)
Write a function called doubleNumber that takes a number and prints double its value. Then call your function with the number 6.
Try It Yourself (Python)
Write a function called double_number that takes a number and prints double its value. Then call your function with the number 6.