Functions - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Functions

Description:

When a program starts, main() is called automatically. ... cout 'Goodbye.n'; if(WhichMessage 1) cout 'I'm confused.n' Variable Scope ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 24
Provided by: tom8
Category:

less

Transcript and Presenter's Notes

Title: Functions


1
Functions

2
What is a Function?
  • A subprogram that can act on data and return a
    value.
  • Every C/C program has at least one function
    main().
  • When a program starts, main() is called
    automatically.
  • main() might call other functions, which might
    call other functions, etc.

3
Func1() Statement Statement return
main() Statement Func1() Statement
Func2() Statement Func4() Statement
return
Func3() Statement Statement Statement
return
Func2() Statement Func3() Statement
return
Func4() Statement Statement return
4
Declaring and Defining Functions
  • The declaration tells the compiler the name,
    return type, and parameters of the function. It
    is analogous to declaring a variable.
  • The definition tells the compiler how the
    function works.

5
Declaring a Function
  • The declaration of a function is called its
    prototype
  • Three ways to declare a function
  • Write your prototype into a file, then use the
    include directive to include it in your program.
  • Write your prototype into the file in which the
    function is used
  • Define the function before it is called by any
    other function. The definition acts as its own
    prototype

6
Function Prototypes
parameter name
return type
int Area (int length, int width) int Area (int,
int)
function name
parameter type
7
Defining a Function
  • The definition consists of the function header
    and the body.
  • The header is exactly like the prototype, except
    that the parameters must be named, and there is
    no terminating semicolon.
  • The body is a set of statements enclosed in
    braces.

8
Declarations and Definitions
int Area(int length, int width) void
PrintMessage(int WhichMessage) int Area(int
length, int width) return length
width void PrintMessage (int WhichMessage)
if(WhichMessage 0) cout ltlt
Hello.\n if(WhichMessage 1)
cout ltlt Goodbye.\n if(WhichMessage gt 1)
cout ltlt Im confused.\n
9
Variable Scope
  • A variable has a scope, which determines how long
    it is available to your program and where it can
    be accessed.
  • Variables declared within a block are scoped to
    that block
  • they can be accessed only within that block
  • and go out of existence when that block ends

10
Local and Global Variables
  • Variables declared within a function exist only
    within the function itself
  • Variables defined outside of any function have
    global scope and thus are available from any
    function in the program

11
Local Variables
  • Function parameters, and variables declared
    within the function, are local to the function

include ltiostream.hgt float Convert(float) int
main() float TempFerIn, TempCel cout ltlt
Enter temp in Farenheit cin gtgt
TempFerIn TempCel Convert(TempFerIn)
cout ltlt Celsius ltlt TempCel gtgt endl return
0 float Convert(float TempFer) float
TempCel TempCel ((TempFer - 32) 5) / 9
return TempCel
12
Global Variables
  • Variables defined outside of any function have
    global scope and thus are available from any
    function in the program.
  • A local variable with the same name as a global
    variable
  • hides the global variable
  • changes in value do not affect the global
    variable

13
Demonstrating global and local variables
include ltiostream.hgt void MyFunc() //
prototype int x 5, y 7 // global
variables void main() cout ltlt x from main
ltlt x ltlt \n cout ltlt y from main ltlt y ltlt
\n\n MyFunc() cout ltlt Back from
MyFunc!\n\n cout ltlt x from main ltlt x ltlt
\n cout ltlt y from main ltlt y ltlt
\n\n void MyFunc() int y 10 cout
ltlt x from MyFunc ltlt x ltlt \n cout ltlt y
from MyFunc ltlt y ltlt \n\n
14
Variables scoped within a block
void func() int x 8 // local x cout ltlt
local x ltlt x ltlt endl cout ltlt still
local x ltlt x ltlt endl int x 9 //
very local x cout ltlt very local x ltlt
x ltlt endl cout ltlt back to local x
ltlt x ltlt endl
15
Functions as Parameters to Functions
  • Legal, but not smart
  • Better

Answer double(triple(square(cube(x))))
int x 2 int cubed cube(x) int squared
square(cubed) int tripled triple(squared) int
Answer double(tripled)
16
Parameter passing by value
  • Changes to passed parameters do not affect values
    in the calling function

void main() int x 5, y 6 swap(x,
y) cout ltlt x ltlt x ltlt y ltlt y ltlt
endl void swap(int x, int y) int temp
temp x x y y temp cout ltlt
x ltlt x ltlt y ltlt y ltlt endl
17
Return
return return 5 return x return (x gt
5) return MyFunc() int Doubler(int original)
if (original lt 10000) return original
2 else return -1 cout ltlt You
cant get here!\n
18
Default Parameters
int VolCube(int len, int wid25, int
hei1) void main() int length100,
width50, height2, area area
VolCube(length, width, height) area
VolCube(length, width) area
VolCube(length) area VolCube(length,
height) // error return int VolCube(int
len, int wid, int hei) return (len wid
hei)
19
Overloading FunctionsFunction Polymorphism
  • An inelegant way of allowing functions to take
    any kind of parameter type.

short DoubleShort(short) int DoubleInt(int) floa
t DoubleFloat(float) double DoubleDouble(double)
short Double(short) int Double(int) float
Double(float) double Double(double)
20
Demonstration of function polymorphism
void main() short MyShort 6500,
DoubledShort int MyInt 65000,
DoubledInt float MyFloat 6.5F,
DoubledFloat double MyDouble 6.5e20,
DoubledDouble DoubledShort
Double(MyShort) DoubledInt
Double(MyInt) DoubledFloat
Double(MyFloat) DoubledDouble
Double(MyDouble) short Double(short
original) return 2 original int
Double(int original) return 2 original
float Double(float original) return 2
original double Double(double original)
return 2 original
21
Inline Functions
inline int Double(int original) void main()
int target 2 target Double(target)
target Double(target) target
Double(target) int Double(int original)
return 2 original
22
Recursion
  • Recursion is when a function calls itself,
    directly or indirectly

23
Factorial Recursion
int Factorial(int x) int i, temp 1
for(i x i gt 1 i--) temp i
return temp int Factorial(int x) if(x lt
2) return 1 return (x Factorial(x
- 1))
Write a Comment
User Comments (0)
About PowerShow.com