0% found this document useful (0 votes)
9 views

W2_OOP

Uploaded by

hoannguyendoduc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

W2_OOP

Uploaded by

hoannguyendoduc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to C++

Programming

Functions
Program Components in C++
• Modules: functions and classes
• Programs use new and “prepackaged” modules
• New: programmer-defined functions, classes
• Prepackaged: from the standard library
• Functions invoked by function call
• Function name and information (arguments) it needs
• Function definitions
• Only written once
• Hidden from other functions

2
Functions
• Functions
• Modularize a program
• Software reusability
• Call function multiple times
• Local variables
• Known only in the function in which they are defined
• All variables declared in function definitions are local variables
• Parameters
• Local variables passed to function when called
• Provide outside information

3
Create a Function

Call a Function
Call a Function

A function can be called multiple times


Function Declaration and Definition

A C++ function consists of two parts:

Declaration: the return type, the name of the function, and


parameters (if any)
Definition: the body of the function (code to be executed)
Function Declaration and Definition
Function Declaration and Definition
Parameters and Arguments

Parameters
Local variables passed to function when called
Provide outside information
Parameters and Arguments
Default Parameter Value
Multiple Parameters
The Return Keyword
The Return Keyword
The Return Keyword

store the result in a variable


Pass By Reference
Pass Array to a Function
Pass Arrays as Function Parameters
Overloading

With function overloading, multiple functions can have the


same name with different parameters
Overloading
Variable Scope
In C++, variables are only accessible inside the region they
are created. This is called scope.
Local Scope
A variable created inside a function belongs to the local
scope of that function, and can only be used inside that
function.
=> A local variable cannot be used outside the function it
belongs to.
Global Scope
A variable created outside of a function, is called a global
variable and belongs to the global scope.
=> A variable created outside of a function is global and can
therefore be used by anyone.
Naming Variables
One available in the global scope (outside the function) and one available in the
local scope (inside the function).
Recursion
Recursion is the
technique of making a
function call itself. This
technique provides a
way to break
complicated problems
down into simple
problems which are
easier to solve.
Recursion vs. Iteration
Repetition
Iteration: explicit loop
Recursion: repeated function calls
Termination
Iteration: loop condition fails
Recursion: base case recognized
Both can have infinite loops
Balance between performance (iteration) and good software
engineering (recursion)
Keyword inline
Inline functions before function
Discuss

1. Function Templates
2. References and Reference Parameters
3. Pointers
4. Header Files

You might also like