CSE 230
Intermediate Programming
in C and C++
Functions
Fall 2017
Stony Brook University
Instructor: Shebuti Rayana
shebuti.rayana@stonybrook.edu
http://www3.cs.stonybrook.edu/~cse230/
Concept of Functions in C
■ The heart of effective problem solving is problem
decomposition. Taking a problem and breaking it into small,
manageable pieces is critical to writing large programs.
■ In C, the function construct is used to implement this "top-
down" method of programming.
■ A program consists of one or more files, each file containing
zero or more functions, one of them being a main() function.
■ Functions are defined as individual objects that cannot be
nested.
■ Program execution begins with main(), which can call other
functions, including library functions such as printf() and
scanf().
■ Functions operate with program variables, and which of these
variables is available at a particular place in a function is
determined by scope rules.
Shebuti Rayana (CS, Stony Brook University) 2
Why Functions?
■ There are several reasons to write programs as
collections of many functions. It is simpler to correctly
write a small function to do one job.
■ Both the writing and debugging are made easier.
■ It is also easier to maintain or modify such a
program. One can readily change just the set of
functions that need to be rewritten, expecting the rest
of the code to work correctly.
■ Small functions tend to be self documenting and
highly readable.
Shebuti Rayana (CS, Stony Brook University) 3
Overview
■ Function definition
■ Function declaration
■ Scope rules
■ Storage classes
■ Recursion
Shebuti Rayana (CS, Stony Brook University) 4
Function Definition
■ The C code that describes what a function does is
called the function definition. It must not be confused
with the function declaration.
■ General Form
return_type function_name(parameter list){
declarations statements }
– Everything before braces comprise header of the
function definition
– Everything between braces comprise the body of the
function definition
– Parameter list is comma separated list of
declarations
Shebuti Rayana (CS, Stony Brook University) 5
Example 1: Function Definition
• The first int tells the compiler that the value returned by the function will be
converted, if necessary, to an int.
• The parameter list consists of the declaration int n. This tells the compiler
that the function takes a single argument of type int.
• An expression such as factorial(7) causes the function to be invoked, or
called.
• The effect is to execute the code that comprises the function definition, with n
having the value 7.
• Finally, the function will return the factorial value of 7.
Shebuti Rayana (CS, Stony Brook University) 6
Example 2: Function Definition
• The first void tells the compiler that this function returns no value; the
second void tells the compiler that this function takes no arguments.
• Following expression causes the function to be invoked.
wrt_address();
• For example, to call the function three times we can write
int i;
for (i = 0; i < 3; i++)
wrt_address();
Shebuti Rayana (CS, Stony Brook University) 7
Function Definition (cont.)
■ If no return type is specified, then it is int by default
■ Any variables declared in the body of a function are
called local variables to that function.
■ Other variables declared external to the function are
called global variables.
Shebuti Rayana (CS, Stony Brook University) 8
The return statement
■ The return statement may or may not contain an expression
return; OR return expression;
■ Example:
return; return 1; return ++a; return (a*b);
■ When a return statement is encountered, execution of the
function is terminated and control is passed to the caller.
■ If the return contains an expression, then the value of the
expression is passed to the caller as well.
■ There can be zero or more return statements in a function.
■ When there is not return, control passed to the caller when
the closing brace is encountered.
Shebuti Rayana (CS, Stony Brook University) 9
Function prototype
■ A function should be declared before it is used.
■ C provides a function declaration syntax called function
prototype
■ A function prototype tells the compiler the number and
type of arguments that are to be passed to the function
and the type of the value that is to be returned by the
function.
■ Example: double sqrt(double);
■ General: type function_name(parameter type list);
Shebuti Rayana (CS, Stony Brook University) 10
Example:
Function
Prototype
Shebuti Rayana (CS, Stony Brook University) 11
Overview
■ Function definition
■ Function declaration
■ Scope rules
■ Storage classes
■ Recursion
Shebuti Rayana (CS, Stony Brook University) 12
Function Declaration
■ From the compilers viewpoint, function declaration
can be generated in various ways. By
– function invocation
– function definition
– explicit function declaration
– function prototype
If a function call, say f(x), is
encountered before any declaration,
definition, or prototype for it occurs,
then the compiler assumes a default
declaration of the form int f()
This code compiles in gcc but
returns garbage value
Shebuti Rayana (CS, Stony Brook University) 13
Alternate
Style of
Function
Definition
Shebuti Rayana (CS, Stony Brook University) 14
Function Invocation
■ Program execution always
begins with main().
■ When program control
encounters a function name,
the function is called, or
invoked. This means that
program control passes to
that function.
■ After the function does its
work, program control is
passed back to the calling
environment, which then
continues with its work.
Shebuti Rayana (CS, Stony Brook University) 15
Call-by-value
■ If a function is “called by value”
– This means that each argument is evaluated, and its
value is used locally in place of the corresponding
formal parameter.
– Thus, if a variable is passed to a function, the stored
value of that variable in the calling environment will
not be changed.
Shebuti Rayana (CS, Stony Brook University) 16
Example: Call-by-value
Shebuti Rayana (CS, Stony Brook University) 17
Call-by-reference
■ "call-by-reference" is a way of passing addresses
(references) of variables to a function that then
allows the body of the function to make changes to
the values of variables in the calling environment.
Call by reference
Call by value
Output: Output:
i = 5 i = 10
j = 10 j = 5
Shebuti Rayana (CS, Stony Brook University) 18
Overview
■ Function definition
■ Function declaration
■ Scope rules
■ Storage classes
■ Recursion
Shebuti Rayana (CS, Stony Brook University) 19
Scope Rules
■ The basic rule of scoping is that identifiers are
accessible only within the block in which they are
declared.
■ They are unknown outside the boundaries of that
block.
Shebuti Rayana (CS, Stony Brook University) 20
Scope Rules for Nested Blocks
Shebuti Rayana (CS, Stony Brook University) 21
Overview
■ Function definition
■ Function declaration
■ Scope rules
■ Storage classes
■ Recursion
Shebuti Rayana (CS, Stony Brook University) 22
Storage Classes
■ Every variable and function in C has two attributes:
– type
– storage class
■ The four storage classes are automatic, external,
register, and static
■ Specifiers: auto, extern, register, static
■ Most common storage class is automatic
Shebuti Rayana (CS, Stony Brook University) 23
Storage class: auto
■ Variables declared within function bodies are
automatic by default
■ Variables defined at the beginning of a block are also
automatic by default
■ We can also explicitly specify automatic variables
auto int a;
■ When execution enters a block, system allocates
memory for the automatic variables
■ When execution exits the block, system releases the
memory allocated for automatic variables (so the
values are lost)
Shebuti Rayana (CS, Stony Brook University) 24
Storage class: extern
■ One method of transmitting information across blocks
and functions is to use external variables.
■ When a variable is declared outside of a function,
storage is permanently assigned to it, and the
storage class is extern.
• extern variable is
considered global
for all the functions
declared after it.
• Never disappears
Shebuti Rayana (CS, Stony Brook University) 25
Storage class: extern
The keyword extern is used to tell the compiler to look
for it elsewhere, either in this file or in some other file.
Shebuti Rayana (CS, Stony Brook University) 26
Storage class: register
■ Tells the compiler to store the associated variables in
high speed memory registers.
■ Defaults to automatic if compiler is unable to store
(resource limitations) the variable into a high-speed
memory register.
■ It is used to improve execution speed.
■ When speed is a concern, programmer may choose
a few variables that are frequently accessed and
declare them to be of storage class register.
Shebuti Rayana (CS, Stony Brook University) 27
Storage class: register
■ If a storage class register is specified in a
declaration and the type is absent, then the type int
by default.
■ Note that in our example the register variable i was
declared as close to its place of use as possible. This
is to allow maximum availability of the physical
registers, only when needed.
■ Always remember that a register declaration is taken
only as advice to the compiler.
Shebuti Rayana (CS, Stony Brook University) 28
Storage class: static
■ Storage class static has two distinct usage
1. The more elementary use is to allow a local variable
to retain its previous value when the block is
reentered.
Ø This is contrast to ordinary automatic variables, which lose their
value upon block exit and must be reinitialized.
Example
The first time the function is invoked,
the variable cnt is initialized to zero.
On function exit, the value of cnt is
preserved in memory. Whenever the
function is invoked again, cnt is not
reinitialized.
Shebuti Rayana (CS, Stony Brook University) 29
Static External Variable
■ The second and more subtle use of static is in
connection with external declarations.
■ They are scope restricted external variables.
■ The scope is the remainder of the source file in which
they are declared.
• Thus, they are
unavailable to functions
defined earlier in the
file or to functions
defined in other files,
even if these functions
attempt to use the
extern storage class
keyword.
Shebuti Rayana (CS, Stony Brook University) 30
Example: Static External Variable
Shebuti Rayana (CS, Stony Brook University) 31
Default Initialization
■ In C, both external variables and static variables that
are not initializes by the programmer are initialized to
zero by the system.
Shebuti Rayana (CS, Stony Brook University) 32
Static Functions
■ This causes scope of the function to be restricted
■ The static functions are only visible within the file in
which they are defined
■ They can not be accessed from other files
Shebuti Rayana (CS, Stony Brook University) 33
Limitations of Function Definition and
Prototypes
■ The function storage class specifier, if present, can
be either extern or static, but not both
■ auto and register cannot be used
■ The types "array of... " and "function returning ... "
cannot be returned by a function. However, a pointer
representing an array or a function can be returned.
■ The only storage class specifier that can occur in the
parameter type list is register.
Shebuti Rayana (CS, Stony Brook University) 34
Overview
■ Function definition
■ Function declaration
■ Scope rules
■ Storage classes
■ Recursion
In next class
Shebuti Rayana (CS, Stony Brook University) 35