Friday, November 21, 2008

Learnigs of the week- November 17-21,2008


STRUCTURED PROGRAMMING USING FUNCTIONS

• A function is a self-contained block of code with a specific purpose.

• A function has a name that both identifies it and is used to call it for execution in a program.

• Functions which perform different actions should generally have different names.


Why Do You Need Functions?

• Functions can be executed as many times as necessary from different points in a program.

• Without the ability to package a block of code into a function, programs would end up being
much larger, since you would typically need to replicate the same code at various points in
your program.

• But the real reason that you need functions is to modularize your program into easily
manageable chunks.

Functions


• Functions allow a program to be segmented so that it can be written piecemeal, and each piece tested independently before bringing it together with the other pieces.

• It also allows the work to be divided among members of a programming team, with each team
member taking responsibility for a tightly specified piece of the program, with a well defined
functional interface to the rest of the code.

• Real world large scale programming projects are developed using this approach

• The name of a function is governed by the same rules as those for a variable.

• You pass information to a function using its arguments which are specified when you invoke
or call it.

• The arguments need to correspond with parameters appearing in the definition of the function.

• The arguments replace the parameters used in the function definition when the function is executed.

• The code in the function then executes as though it was written using your argument values.


Structure of Functions using Function Definition

• A Function Definition is the “self-contained block of code with a specific purpose”.

• A functions definition has to two distinct parts: a function header, followed by the body of the function.

• Functions Header which identifies the function – Tells the compiler what the function is and its return
type

• Function Body between curly braces containing the executable code for the function
– the actual executable part of the function.


The General Form of a Function Header

• Function header consists of three parts:
– the type of the return value
– the name of the function,
– the parameters of the function enclosed between
parentheses.

• The general form of a function header can be written as
follows:
return_type
FunctionName(parameter_list)

• No semicolon is required at the end of the function header.

Function Prototypes


• Before you can use a function in a program, you must declare the function using a statement called a function prototype.

• Function prototype informs the compiler of the framework for that function.

• A prototype of a function provides the basic information that the compiler needs to check that a function is used correctly.

• It specifies:
– the parameters to be passed to the function,
– the function name, and
– the type of the return value;

• it contains essentially the same information as appears in the function header, with the addition
of a semicolon.

• a semicolon is required at the end of a function prototype.

• The number of parameters and their types must be the same in the function prototype as they are in the function header in the definition of the function.

• The prototypes for the functions used in a program must appear before the statements calling the functions.

Naming of Parameters in function header and prototype

• A parameter name in the function prototype should provide an understanding about why it is
used.

• You can specified different names for the parameters in the function prototype to those
used in the function header when you defined the function.

• Most often, the same names are used in the prototype and in the function header in the
definition of the function, but this doesn't have to be so.

Header Files for Function


Prototypes

• Header files is a C++ files that has a .h extension, appear between <> in a #include statement.

• The header files which we've been including for standard library functions include the prototypes of the functions provided by the library.

• If your code uses a lot of functions, it is a good idea to place all function prototypes in a header
file.

• In the header file, the function prototypes are stored along with a brief description of the
purpose of the function.

What is returned by a Function


• A function communicates back with the rest of the program using its return statement

• The return_type can be any legal type.

• The return value is returned to the calling function when the function is executed, so when
the function is called

• The return type will have the same type as in the declaration of the function.

• A copy of the value being returned is made automatically, it is available to the return point in
the program.


The return Statement


• The return statement returns a value to the calling program at the point where the function

• The general form of the return statement is as follows, return expression;

• expression must evaluate to a value of the type specified in the function header for the return value.

• The expression can be any expression you want, as long as you end up with a value of the required type.

• If the type of return value has been specified as void, there must be no expression appearing in the return statement. It must be written simply as: return


Keyword void used in functions


• If the function does not return a value, the return type is specified by the keyword void.

• The keyword void is also used to indicate that a function has no parameters and doesn't return a value void MyFunction(void)

• An empty parameter list also indicates that a function takes no arguments, so you could omit the keyword void between the parentheses as follows: void MyFunction()

• A function with a return type specified as void should not be used in an expression in the calling program.

Function Body


• Functions should begin with { and end with }.

• All the variables declared within the body of a function, as well as the parameters, are local to the function.

• The scope of variables declared within a function is determined in the same way that we have already discussed.

• A variable is created at the point at which it is defined and ceases to exist at the end of the block containing it.

• Be careful about masking global variables with local variables of the same name.


Function Arguments


• Parameters are used in the function prototype but functions receive input from the rest of the
program using arguments.

• Arguments can be any expression, or variables as long as a value of the correct type is
ultimately produced.

• The arguments substitute the parameters that were used in the definition of the function.


Calling a Function


• When you call a function, you are telling the compiler branch off and execute the piece of code
that defines the function.

• The computation is performed using these values, then a copy of the result, will be returned to the calling function which stores it in the appropriate argument.

• You can think of the function as having this value in the statement or expression in which it appears

• To call a function, use the function name followed by arguments enclosed in parenthesis.


Passing Arguments to a Function


• The arguments specified when a function is called should usually correspond in type and
sequence to the parameters appearing in the definition of the function.

• If the type of an argument specified in a function call doesn't correspond with the type
of parameter in the function definition, then (where possible) it will be converted to the
required type.

Methods of transferring data to a function

• Pass-by-value method of transferring data to a function

– With this mechanism, the variables or constants that you specify as arguments are not passed to a function at all. Instead, copies of the arguments are created and these copies are used as the values to be transferred.

• Pass-by-reference method of transferring date to a function.

– The method used is not pass-by-value where an argument is copied before being passed, but pass-by reference where the parameter acts as an alias for the argument passed.


Pass-by-value method

• Each time you call a function, the compiler arranges for copies of the arguments that you
specify to be stored in a temporary location in memory.

• During execution of the functions, all references to the function parameters will be mapped to these temporary copies of the arguments.

• Clearly, the pass-by-value mechanism provides you with a high degree of protection from having your caller arguments mauled by a rogue function, but it is conceivable that we might actually want to arrange to modify caller arguments.


Passing Arrays to functions

• You can also pass an array to a function, but in this case the array is not copied, even though a pass-by value method of passing arguments still applies.

• The array name is converted to a pointer (is an address of a variable), and a copy of the pointer to the beginning of the array is passed to the function.

• This is quite advantageous, as copying large arrays could be very time and memory consuming.

• To pass an array to a function, just specify the name of the array in the argument list.

• In Multidimensional Arrays follow the same procedure


Other methods of communicating with a function

• Specifying a parameter to a function as a reference changes the method of passing data
for that parameter.

• This eliminates any copying and allows the function to access the caller argument directly.

• It also means that the de-referencing, which is required when passing and using a pointer to a
value, is also unnecessary.

• We will revisit this a method after we introduce pointers and reference



by: Hanna Deborrah T. Ongking IV- Rizal

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home