Saturday, December 6, 2008

Learnings of the Week [December 1-5, 2008]

In this week, we tackled about the Sample Problems Involving Function Calls.

A function call is an expression containing a simple type name and a parenthesized argument list. The argument list can contain any number of expressions separated by commas. It can also be empty.

For example:

stub()
overdue(account, date, amount)
notify(name, date + 5)
report(error, time, date, ++num)

There are two kinds of function calls: ordinary function calls and C++ member function calls. Any function may call itself except for the function main.

Type of a Function Call

The type of a function call expression is the return type of the function. This type can either be a complete type, a reference type, or the type void. A function call is an lvalue if and only if the type of the function is a reference.

Arguments and Parameters

A function argument is an expression that you use within the parentheses of a function call. A function parameter is an object or reference declared within the parentheses of a function declaration or definition. When you call a function, the arguments are evaluated, and each parameter is initialized with the value of the corresponding argument. The semantics of argument passing are identical to those of assignment.

A function can change the values of its non-const parameters, but these changes have no effect on the argument unless the parameter is a reference type.


Example of Function Calls

In the following example, main passes func two values: 5 and 7. The function func receives copies of these values and accesses them by the identifiers: a and b. The function func changes the value of a. When control passes back to main, the actual values of x and y are not changed. The called function func only receives copies of the values of x and y, not the variables themselves.

/**
** This example illustrates function calls
**/

#include

void func (int a, int b)
{
a += b;
printf("In func, a = %d b = %d\n", a, b);
}

int main(void)
{
int x = 5, y = 7;
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}

This program produces the following output:

In func, a = 12   b = 7
In main, x = 5 y = 7



Posted by:
RAE ANGELINE S. PALEN
IV - Rizal

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home