Chapter 3 Modular Programming, Lecture notes of Programming Paradigms

Creating user-defined functions Parameter Passing Scope and Lifetime of variables Recursion

Typology: Lecture notes

2020/2021

Available from 04/02/2022

gwen-hermo
gwen-hermo 🇵🇭

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 3
Modular Programming
Creating user defined functions
C++ provides a mechanism similar to any other programming language that
would create modules of the program called functions.
the main () function - responsible for calling other functions although any
function can call any other function.
- Avoid task duplication
- Make large tasks manageable
o functionPrototype - tells the compiler what the function has,
the type of return value and the number of function arguments.
o returnDataType - be given back once the function would
finish its operation.
o functionName - identifier used so that the function can be
invoked (or called); begin a function name with an action word.
o parameterList - contain the information that would be given to
function once it is invoked.
o Statements - executable task bounded within curly braces.
o Return - actual value that is returned as a result of the process
given by the function.
Void function - nullify any form of data type with its corresponding value that is
returned from a function.
pf3
pf4
pf5

Partial preview of the text

Download Chapter 3 Modular Programming and more Lecture notes Programming Paradigms in PDF only on Docsity!

Chapter 3

Modular Programming

Creating user defined functions ➢ C++ provides a mechanism similar to any other programming language that would create modules of the program called functions. ➢ the main () function - responsible for calling other functions although any function can call any other function.

  • Avoid task duplication
  • Make large tasks manageable o functionPrototype - tells the compiler what the function has, the type of return value and the number of function arguments. o returnDataType - be given back once the function would finish its operation. o functionName - identifier used so that the function can be invoked (or called); begin a function name with an action word. o parameterList - contain the information that would be given to function once it is invoked. o Statements - executable task bounded within curly braces. o Return - actual value that is returned as a result of the process given by the function.
  • Void function - nullify any form of data type with its corresponding value that is returned from a function.
  • Value Returning function - indicate that the particular function would return a value of that datatype o Argument - value that a function would begin with o value returned - the result of the processing when a function has finished execution ✓ A function may have many arguments but only one value returned. Parameter Passing (https://www.geeksforgeeks.org/parameter-passing-techniques-in-c- cpp/) ➢ parameter allows the sending of values to the function’s internal operation o actual parameter – argument(s) that are given to a function o formal parameter - actual parameter is accepted on the parameter block of the function
  • Pass by Value - the content of the variable assigned on the actual parameter is copied to the variable on the formal parameter. Meaning that the changes made of the variables within the function are not reflected on the invoking function.
  • Pass by Reference - (reference parameters) link the address of the variable from the calling function to the function that is invoked.
  • Precautions in Parameter passing
    1. There must be the same number of actual parameters to formal parameters except for default parameter. 2. To prevent data loss and subsequent error for overloaded function, the data type for each actual parameter must be the same with the datatype of formal parameter. 3. When passing by reference the actual parameter must be a variable , whereas the actual parameter when passing by value may be a variable, constant or expression Using return values as Boolean Parameters of Control StructuresBoolean return type can be used to replace the complex statement thus reducing the amount of code and the possibility of an error

Scope and Lifetime of variables

  • Scope - variable is used for a particular program
  • Lifetime - how long a variable would remain in the internal memory of the computer
  • Local variables - has a scope to that particular function in which it is declared; when the function would finish execution , the variable would be released from the memory.
  • Global variables - variables that are declared outside any function; it would only be released from the memory once the program would end. Overloading Functions - Some function in C++ may have the same name but is unique on their formal parameter
  1. They must have different number of formal parameters.
  2. If the number of formal parameters are the same, they must at least differ on the order of listing
  • Automatically allocate local variables for each function call.