Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

6 - Elements of Programming Part 4

The document discusses elements of programming including functions, modular programming, and advantages of using modular programming. It defines functions, explains how functions work, and provides examples of defining and calling functions. It also covers topics like parameters, passing values vs references, and good programming practices related to functions.

Uploaded by

Christian Moya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

6 - Elements of Programming Part 4

The document discusses elements of programming including functions, modular programming, and advantages of using modular programming. It defines functions, explains how functions work, and provides examples of defining and calling functions. It also covers topics like parameters, passing values vs references, and good programming practices related to functions.

Uploaded by

Christian Moya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

ELEMENTS OF

PROGRAMMING
Part 4
To explain modular programming,
To enumerate other built-in
functions,
And to create functions
Programs in the real world can
be extremely large.
FUNCTIONS
for Modular Programming
FUNCTIONS
aka subroutines
FUNCTIONS
aka procedures
FUNCTIONS
aka modules
We divide this into small
manageable parts
(and one each performs a task).
A large program typically has many
other functions which are
invoked/called by main() or other
functions.
Predefined functions in C
Example: Generating random
numbers
Defining functions in C
Example #1
How does this work?
If a function is called,
execution is transferred
to that function.
That function then
executes its code.
Once the function finishes
or meets a return statement,
control is transferred back.
Example #2
Return now actually returns
something (sum).
c = addUp(a, b);

int addUp(int x, int y)


{
int sum = x + y;
return sum;
}
c = addUp(a, b);

“int x=a, int y=b”

int addUp(int x, int y)


{
int sum = x + y;
return sum;
}
Create two functions that return the
square and the cube of a floating point
number x.
Create a function that finds and returns
the larger of two integers.
Create a function that finds and returns
the larger of three integers.
Advantages of using modular
programming
It avoids redundancy.
Lengthy code that is repeated at
different parts of a program need to
be written only once.
It encourages reusability.
Frequently-used functions can be
added to a library.
It improves readability.
Implementation details are hidden
in the functions.
It helps manage
complexity.
Large software eng’g projects are
split into logical modules that can
be developed and tested
separately.
Good programming style w/
functions
Use predefined functions
when available.
Use appropriate
parameters for your
functions.
Use only local variables for your
functions to make them as self-
contained as possible.
PARAMETERS
Parameters and the return value
communicate between the caller
and the function.
Parameters = Input
Return = Output
A parameter can be changed by
passing the address of a variable
instead of its value.
Pass by Value
vs
Pass by Reference
The value of n is unchanged after
the function call to foo().
The value of n is changed after the
function call to goo().

You might also like