Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Modular Programming in C++: Functions, Parameters, and Advanced Techniques - Prof. Rose, Study notes of Computer Science

This document offers a comprehensive guide to modular programming in c++, covering key concepts such as functions, parameters, and advanced techniques like inline functions and function overloading. it explains how to define and call functions, pass data by value and reference, and utilize default arguments. The document also includes examples to illustrate these concepts, making it a valuable resource for students learning c++ programming.

Typology: Study notes

2024/2025

Available from 05/13/2025

r22syedamubarrahmpcs029
r22syedamubarrahmpcs029 🇮🇳

5 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT I Chpt II - Functions
Modular Programming
Modular programming: breaking a program up into smaller, manageable functions or
modules
Function: a collection of statements to perform a task
Motivation for modular programming:
Improves maintainability of programs
Simplifies the process of writing programs
User defined Functions
In order to create a user defined function we need to have three elements in a program i.e
function prototype/declaration, function call and function definition.
Function call: statement causes a function to execute
Function definition: statements that make up a function
Definition includes:
return type: data type of the value that function returns to the part of the program
that called it
name: name of the function. Function names follow same rules as variables
parameter list: variables containing values passed to the function
body: statements that perform the function’s task, enclosed in {}
Calling a Function:
A function call is giver inside the main block.
To call a function, use the function name followed by () and ;
printHeading();
When called, program executes the body of the called function
After the function terminates, execution resumes in the calling function at point of
call.
Function Prototypes:
Ways to notify the compiler about a function before a call to the function:
Use a function prototype (function declaration) like the function definition
without the body
Prototype: void printHeading();
Place prototypes near top of program
Program must include either prototype or full function definition before any call to the
function compiler error otherwise
pf3
pf4
pf5

Partial preview of the text

Download Modular Programming in C++: Functions, Parameters, and Advanced Techniques - Prof. Rose and more Study notes Computer Science in PDF only on Docsity!

UNIT I – Chpt II - Functions

Modular Programming

  • Modular programming: breaking a program up into smaller, manageable functions or modules
  • Function: a collection of statements to perform a task
  • Motivation for modular programming:
    • Improves maintainability of programs
    • Simplifies the process of writing programs User defined Functions
  • In order to create a user defined function we need to have three elements in a program i.e function prototype/declaration, function call and function definition.
  • Function call: statement causes a function to execute
  • Function definition: statements that make up a function
  • Definition includes:
  • return type: data type of the value that function returns to the part of the program that called it
  • name: name of the function. Function names follow same rules as variables
  • parameter list: variables containing values passed to the function
  • body: statements that perform the function’s task, enclosed in {}
  • Calling a Function:
  • A function call is giver inside the main block.
  • To call a function, use the function name followed by () and ; printHeading();
  • When called, program executes the body of the called function
  • After the function terminates, execution resumes in the calling function at point of call.
  • Function Prototypes :
  • Ways to notify the compiler about a function before a call to the function:
  • Use a function prototype (function declaration) – like the function definition without the body
  • Prototype: void printHeading();
  • Place prototypes near top of program
  • Program must include either prototype or full function definition before any call to the function – compiler error otherwise

Example Program: Passing Data by Value

  • Pass by value: when an argument is passed to a function, its value is copied into the parameter.
  • Changes to the parameter in the function do not affect the value of the argument Example Program:
  • Space between type and & is unimportant
  • Must use & in both prototype and header
  • Argument passed to reference parameter must be a variable – cannot be an expression or constant
  • Use when appropriate – don’t use when argument should not be changed by function, or if function needs to return only 1 value Example Program: Inline Functions C++ provides an inline functions to reduce the function call overhead. Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time. Inline function may increase efficiency if it is small. The syntax for defining the function inline is: inline return-type function-name(parameters) {

// function code } Default Arguments A Default argument is an argument that is passed automatically to a parameter if the argument is missing on the function call.

  • Must be a constant declared in prototype: void evenOrOdd(int = 0);
  • Can be declared in header if no prototype
  • Multi-parameter functions may have default arguments for some or all of them: int getSum(int, int=0, int=0); Overloading Functions
  • Overloaded functions have the same name but different parameter lists
  • Can be used to create functions that perform the same task but take different parameter types or different number of parameters
  • Compiler will determine which version of function to call by argument and parameter lists Example Program: