Lecture #17: Functions 2

November 10, 1999

Bob Pitha

bpitha@cs.ucsd.edu

http://www-cse.ucsd.edu/classes/fa99/cse5a

let’s review functions

•      A function is like a small program within the program

•      You pass data into the function via its arguments

•      You get data back from the function via its return value

•      You call the function using a function call expression

function example

Text Box: /*
 * power: calculate m to the nth power
 */
long power (int m, int n)
{
    int i;
    int product=1;

    for (i = 1; i <= n; ++i)
        product *= m;
    return product;
}
 


function example – arguments


 


arguments

•      The functions argument define variables for the function

–   these variables are only available from inside the function

–   their values are set to whatever values are “passed in” when the function is called

–   these are copies of the values that are passed. Changing the value of an argument does not change the value of the variable whose value was passed in

function example – return value


 


return value

•      The function can return a single value

–   its data type is defined as part of the function declaration

–   if the function is declared as returning a value, it must return a value

–   if you don’t want to return a value, the return type should be void

function call expression

•      You invoke a function when the computer evaluates a function call expression

–   function name followed by arguments in parentheses

–   commas separate multiple arguments

–   if no arguments, just empty parentheses

let’s look at an example

example

Function 1: main()

Function 2: print_heading()

Function 3: print_table()

Function 4: power()

function declaration issues

•      We must declare a function before we can call it

–   we can use a function prototype

Text Box: long power (int, int);
void print_heading (void);
void print_table (int);
 


–   or we can use the function definition as a declaration

•   but only so long as the definition comes before the call

•   so in our example, we used prototypes because our function definitions came after the function calls

example

function definition order

•      But if we had introduced the functions in a different order, we wouldn’t need to use prototypes

–   because the definition also declares the function

example (rearranged)

prototype issues

•      There are some issues here…

–   this can be harder to read

•   since main now comes at the end

•   it’s kind of Reverse Polish Notation for programs

–   it only works within a single file

•   we’ve only seen programs that fit in a single file

•   but we can use multiple .cpp files to write our program

•   every function must be defined in a single file

•   but each function could be in a different file

example

variable declarations

•      We can now revisit variable declarations, and discuss some issues we passed over the first time

variable concepts

•      scope

•      visibility

•      extent

scope

•      The region of the program code in which declaration has effect

–   Local variables: declaration through to end of block

–   Global variables: declaration through to end of file

–   Parameters: through to end of function

what does this mean?

•      It means that when you declare a variable, it doesn’t mean the variable is available everywhere

example – local scope

example – parameter scope

example – global scope