Grade 9 Exam  >  Grade 9 Notes  >  AP Computer Science Principles  >  Chapter Notes: Calling Procedures

Calling Procedures Chapter Notes | AP Computer Science Principles - Grade 9 PDF Download

Introduction

Calling procedures is a key concept in AP Computer Science Principles, allowing programmers to reuse sets of instructions efficiently. This chapter explains what procedures are, how they are defined, and how they are used in programs. It covers the role of parameters and arguments, as well as how to return values from procedures. By understanding procedures, programmers can write cleaner, more organized code without repeating the same instructions multiple times.

Procedures and Procedure Calls

  • A procedure is a group of programming instructions that can be reused, also called a function or method in some languages.
  • Procedures let you use the same code multiple times without rewriting it.
  • In AP Pseudocode:
    • Define a procedure: PROCEDURE procName(parameter1, parameter2, ...) { block of statements }
    • Call a procedure: procName(arg1, arg2, ...), where arguments (arg1, arg2) are assigned to parameters.
    • The procedure can take zero or more parameters.

Question for Chapter Notes: Calling Procedures
Try yourself:What can procedures also be called in programming?
View Solution

Parts of a Procedure

  • Parameters are input variables that a procedure uses to perform its task.
  • Example: A procedure to add two numbers needs two parameters (e.g., first_number, second_number).
  • Not all procedures require parameters.
  • Arguments are the actual values or variables passed to a procedure when it’s called.
  • In Python:
    • Define a function: def name_of_function(parameter1, parameter2): # code statements
    • Example: def summing_machine(first_number, second_number): print(first_number + second_number)
    • Call with arguments: summing_machine(5, 7) outputs 12.
  • When a procedure is called, the program executes its code as if it were written in place, then continues with the next line of code.

Return Statement

  • A return statement lets a procedure send a value back to the code that called it.
  • In AP Pseudocode:
    • PROCEDURE procName(parameter1, parameter2, ...) { block of statements; RETURN expression }
    • The RETURN statement stops the procedure and returns the value of the expression.
    • Assign the returned value: result ← procName(arg1, arg2, ...)
  • In Python:
    • Example: def summing_machine(first_number, second_number): value = first_number + second_number; return value
    • Call and assign: answer = summing_machine(5, 7); print(answer) outputs 12.
    • Use the returned value: print(answer + 1) outputs 13.
  • The return statement can appear anywhere in the procedure and exits it immediately.

Question for Chapter Notes: Calling Procedures
Try yourself:
What keyword is used to define a procedure in Python?
View Solution

Key Terms

  • Arguments: Values provided to a function when it is called, allowing customization of the function’s behavior.
  • Parameters: Variables defined in a function’s declaration that act as placeholders for the arguments passed during a call.
  • Return Statement: A statement that specifies the value a function should send back to the calling code, enabling further use of the result.
The document Calling Procedures Chapter Notes | AP Computer Science Principles - Grade 9 is a part of the Grade 9 Course AP Computer Science Principles.
All you need of Grade 9 at this link: Grade 9
35 docs

FAQs on Calling Procedures Chapter Notes - AP Computer Science Principles - Grade 9

1. What is a procedure in computer science?
Ans. A procedure is a set of instructions that perform a specific task. It allows programmers to encapsulate code for reuse, making programs easier to read and maintain.
2. How do I write a procedure in Python?
Ans. To write a procedure in Python, you use the 'def' keyword followed by the procedure name and parentheses. Inside the parentheses, you can define parameters. For example: python def my_procedure(param1, param2): # code to execute
3. How do I call a procedure in programming?
Ans. To call a procedure, you simply use its name followed by parentheses. If the procedure requires parameters, you must provide the required arguments within the parentheses, like this: `my_procedure(arg1, arg2)`.
4. What is the purpose of the return statement in a procedure?
Ans. The return statement is used to send a value back to the part of the program that called the procedure. This allows the procedure to provide output that can be used further in the program.
5. What are some key terms related to procedures in computer science?
Ans. Key terms include: - Procedure: A block of code that performs a specific task. - Parameter: A variable used in a procedure to accept input. - Argument: The actual value passed to a procedure when it is called. - Return: A statement that sends back a value to the caller of a procedure.
Related Searches

study material

,

Summary

,

Exam

,

past year papers

,

ppt

,

Semester Notes

,

mock tests for examination

,

Important questions

,

Objective type Questions

,

Free

,

shortcuts and tricks

,

Previous Year Questions with Solutions

,

pdf

,

Sample Paper

,

practice quizzes

,

Calling Procedures Chapter Notes | AP Computer Science Principles - Grade 9

,

Calling Procedures Chapter Notes | AP Computer Science Principles - Grade 9

,

Viva Questions

,

Extra Questions

,

video lectures

,

MCQs

,

Calling Procedures Chapter Notes | AP Computer Science Principles - Grade 9

;