Functions and Procedures as Subroutines

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

How do subroutines assist in tackling complex problems?

  • By stringently adhering to sequential programming without deviation.
  • By treating all problems as equally simple.
  • By increasing the complexity of the main program.
  • By breaking down problems into smaller, more manageable sub-problems. (correct)

What are the two primary types of subroutines?

  • Functions and procedures. (correct)
  • Algorithms and data structures.
  • Variables and constants.
  • Integers and strings.

In the example of combination calculation, what is the primary difference between the three parts (calculations of factorials)?

  • The presence or absence of comments.
  • The use of different mathematical formulas.
  • The programming language used.
  • The numbers used in the 'for' loop. (correct)

When defining a function, what is the term for the variables listed inside the parentheses in the function's declaration?

<p>Arguments. (A)</p> Signup and view all the answers

Regarding function calls, how is a function typically treated within an expression?

<p>As an expression of the returned value's type. (A)</p> Signup and view all the answers

When calling a function in algorithm programming, what can the parameters of the function be replaced by?

<p>Global variables of the same types or expressions. (B)</p> Signup and view all the answers

What is a crucial restriction when assigning values in the context of functions?

<p>A function name should never be assigned a value using the '=' assignment symbol. (D)</p> Signup and view all the answers

What distinguishes a procedure from a function?

<p>A procedure is used to execute a task and has no return value, whereas a function returns a value. (D)</p> Signup and view all the answers

What happens if you attempt to assign the returned value of a procedure to a variable?

<p>It will raise an error because procedures do not return values. (C)</p> Signup and view all the answers

What is the scope of a local variable?

<p>Only the subroutine in which it is declared. (B)</p> Signup and view all the answers

When does the creation and destruction of local variables occur?

<p>They are created at the execution of the function and destroyed at the end of the execution. (D)</p> Signup and view all the answers

What happens if a local variable has the same name as a global variable?

<p>The global variable is locally inaccessible. (B)</p> Signup and view all the answers

How are parameters considered in relation to local variables?

<p>They are an exceptional case of local variables known as formal variables. (C)</p> Signup and view all the answers

What distinguishes parameters from typical local variables in subroutines?

<p>Parameters receive an initial value directly from the calling function's argument. (A)</p> Signup and view all the answers

Why is dividing into subroutines important?

<p>It allows linking and collaboration among subroutines to achieve a common task. (C)</p> Signup and view all the answers

Which of the following describes parameter passing?

<p>Sharing data between subroutines to work in unison. (B)</p> Signup and view all the answers

What are the methods for passing parameters to functions?

<p>value, variable, and address. (A)</p> Signup and view all the answers

When a parameter is passed by value, what happens within the function if the value of the formal parameter is modified?

<p>Only the local copy of the parameter is affected, leaving the original variable unchanged. (B)</p> Signup and view all the answers

What is the primary characteristic of a recursive function?

<p>It calls itself during its execution. (D)</p> Signup and view all the answers

What is the role of the 'stopping criterion' in a recursive function?

<p>To prevent infinite loops by providing a base case. (A)</p> Signup and view all the answers

What happens during each iteration of a properly designed recursive function?

<p>It simplifies the overall task or modifies the input, working towards the base case. (D)</p> Signup and view all the answers

In the context of recursion, what does a 'child function' refer to?

<p>The subsequent call to the recursive function. (C)</p> Signup and view all the answers

Why might a programmer choose to use recursion in a function?

<p>To solve problems that can be broken down into smaller, self-similar subproblems. (B)</p> Signup and view all the answers

Which aspects are critical to consider when applying recursion?

<p>Stopping condition, resolution, and recursive call(s). (B)</p> Signup and view all the answers

In recursion, with a stopping condition, resolution, and modification, what should the program be working towards?

<p>An exceptional case simple to process. (C)</p> Signup and view all the answers

What does the 'resolution' phase in recursion primarily involve?

<p>Finding a recursive relationship between different iterations. (C)</p> Signup and view all the answers

Why modify parameters in the code for recursion?

<p>Parameters are always done by modifying its parameters according to the parameters of the parent function. (B)</p> Signup and view all the answers

In the factorial example of recursion, what value is returned when n=0?

<p>1 (D)</p> Signup and view all the answers

When calculating factorial(5) using recursion, which function call returns a particular case with a defined result?

<p>factorial(0) (B)</p> Signup and view all the answers

Flashcards

What is a function?

A named block of code that performs a specific task and returns a value.

What is a procedure?

A named block of code that performs a specific task but does not return a value.

Scope

The part of the program where a variable is accessible.

Global variable

A variable that is accessible from anywhere in the program.

Signup and view all the flashcards

Local variable

A variable that is only accessible within the function or procedure in which it is declared.

Signup and view all the flashcards

Passing by value

Passing a copy of the variable's value to the subroutine. Changes to the parameter within the subroutine do not affect the original variable.

Signup and view all the flashcards

Passing by variable

Giving the subroutine direct access to the original variable. Changes to the parameter within the subroutine do affect the original variable.

Signup and view all the flashcards

Recursion

A programming technique where a function calls itself in order to solve a problem.

Signup and view all the flashcards

Stopping Condition

The condition that must be met in a recursive function to stop the recursion and return a value.

Signup and view all the flashcards

Resolution

Breaking down a problem into smaller subproblems, solving each subproblem recursively, and combining the solutions to solve the original problem.

Signup and view all the flashcards

Recursive call

A function calls itself.

Signup and view all the flashcards

Study Notes

  • The topic considers functions and procedures as subroutines.
  • Dr. Belgroun Brahim from the University of Khenchela created this course for 1st-year MI, Math, & INFO in 2022/2023.

Introduction to Subroutines

  • Tackling complex problems requires breaking them into simpler sub-problems via a bottom-up approach.
  • Programs can be divided into reusable pieces, which can be extracted and processed independently.
  • Subroutines refer to functions and procedures.

Combination Example Overview

  • Combination algorithms can be broken down into similar parts.
  • The key difference lies in numbers within the "for" loop, considered an attribute of the new part.
  • The Fact function calculates the factorial of a number and returns an integer.

Function Details

  • A function is a subroutine that returns a value of a specified type.
  • Functions can accept parameters of various types.
  • When invoked, a function acts as an expression that corresponds to the returned value’s type.

How to Utilize Functions

  • When a function is called, it's treated as an expression with its returned value type.
  • Function parameters can be replaced with global variables of matching types.
  • Expressions with compatible types can also serve as function parameters.
  • The function name should not be assigned a value using the "=" assignment symbol, as the left part of the assignment should be a variable, not a value.

Procedures Defined

  • A procedure is a subroutine for executing a parameterized task and may or may not take inputs, but it does not return a result.

How to Utilize Procedures

  • Procedures entail a series of sequentially executed instructions determined by its parameters.
  • Procedure parameters can be substituted with global variables or expressions that match the parameter types.
  • The procedure name cannot be assigned a value using the "=".
  • The returned value must be ignored; it cannot be included in an expression.

Global vs Local Variables

  • Each variable possesses a scope, representing a code block where it exists and can be modified.
  • Variables can either be local or global.
  • A global variable is defined in the main program, and all subroutines can access it.
  • A local variable's scope is limited to the subroutine in which it is defined and is created, and then destroyed at the end of execution.
  • If a local variable shares a name with a global one, the global variable becomes inaccessible within that routine.
  • Subroutine parameters are exceptional local variables known as "formal variables”.
  • The difference is that formal parameters are initialized with a value which is the call argument provided by the calling function.

Parameter Passing Methods

  • Program division into subroutines allows linked subroutines to work together.
  • This involves exchanging parameters between subroutines to achieve the main program's purpose. Methods to pass parameters include:
    • Passing by value: a copy of the value is passed. Modifications inside the subroutine do not affect the original variable.
    • Passing by variable: the actual variable is passed.
    • Passing by address.

Recursion Defined

  • For particular problems solvable through a recursive approach, the solution of the next situation is generated from existing known solutions.
  • Specifically, recursion is a function that calls itself, which repeats until a stopping criterion is satisfied.
  • The process involves a parent function invoking a child function with modified parameters until the final function reaches the specified condition.
  • It then sends the appropriate result to its parent, ultimately returning a final result to the original function.

Recursive Properties

  • Stopping Condition: Determines the termination point of the recursive calls.
  • Resolution: Involving how the result of a parent function is calculated from its child function.
  • Recursive call(s): Where parameters are modified according to those of the parent function.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser