1 Oopc
1 Oopc
1 Oopc
Lecture (1)
Functions :
Functions are the building blocks of C and C++ and the place where all program activity
occurs. This lecture examines their C-like features, including passing arguments, returning
values, returning pointer and recursion.
The ret-type specifies the type of data that the function returns. A function may return any
type of data except an array. The parameter list is a comma-separated list of variable names
and their associated types that receive the values of the arguments when the function is called.
A function may be without parameters, in which case the parameter list is empty. However,
even if there are no parameters, the parentheses are still required.
Function Arguments :
If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function. They behave
like other local variables inside the function and are created upon entry into the function and
1
Object Oriented Programming
Lecture (1)
destroyed upon exit. As shown in the following function, the parameter declarations occur
after the function name:
int main()
{
int x,y;
scanf(“%d%d”,&x,&y);
sum(x,y);
return 0;
}
2
Object Oriented Programming
Lecture (1)
}
int sqr(int x)
{
x = x*x;
return(x);
}
In this example, the value of the argument to sqr() , 10, is copied into the parameter x. When
the assignment
x = x*x takes place, only the local variable x is modified. The variable t, used to call sqr() ,
still has the value 10. Hence, the output is 100 10. Remember that it is a copy of the value of
the argument that is passed into the
function. What occurs inside the function has no effect on the variable used in the call.
3
Object Oriented Programming
Lecture (1)
parameters) must be called with the addresses of the arguments. The following program
shows the correct way to call swap() :
int main()
{
int i=10, j=20;
swap(&i, &j); /* pass the addresses of i and j */
return 0;
}
In this example, the variable i is assigned the value 10 and j is assigned the value 20. Then
swap() is called with the addresses of i and j. (The unary operator & is used to produce the
address of the variables.) Therefore, the addresses of i and j, not their values, are passed into
the function swap() .
Reference Declarations :
4
Object Oriented Programming
Lecture (1)
pointer variable p is declared and initialized to &i, it has an identity separate from i.
The pointer p has memory associated with it that is initialized to the address of i. When
a reference variable r is declared and initialized to i, it is identical to i. It does not have
an identity separate from the other names for the same object. In effect, r is just
another name for i, that is, an alias.
The following definitions are used to demonstrate the use of dereferencing and aliasing.
The definitions assume that memory at location 1004 is used for integer variable a
and that memory at 1008 is used for pointer variable p.
int a = 5; // declare and define a
int* p = &a; // p points to a
int& ref_a = a; // alias for a
Notice in the preceding figure of pointer declarations that any change to the value of a
is equivalent to changing ref_a. Such a change affects the dereferenced value of p. The
pointer p can be assigned another address and lose its association with a. However, a
and ref_a are aliases and within scope must refer to the same object. These declarations
can be used for call-by-reference arguments, which allows C++ to have call-by-reference
arguments directly. The function order(), using this mechanism, is recoded as
follows:
In file order2.cpp
void order(int& p, int& q)
{
int temp;
if (p > q)
{
temp = p;
p = q;
q = temp;
}
}
int main()
{
int i, j;
5
Object Oriented Programming
Lecture (1)
·····
order(i, j);
·····
}
If i and j are int variables, then order(i, j) uses the reference to i and the reference
to j to exchange, if necessary, their two values. In traditional C, this operation
must be accomplished by using pointers and dereferencing.
All functions, except those of type void, return a value. This value is specified by the
return statement. In C, if a non-void function does not explicitly return a value via a return
statement, then a garbage value is returned. In C++, a non-void function must contain a
return statement that returns a value. That is, in C++, if a function is specified as returning a
value, any return statement within it must have a value associated with it. However, if
execution reaches the end of a non-void function, then a garbage value is returned. Although
this condition is not a syntax error, it is still a fundamental error and should be avoided. As
long as a function is not declared as void, you may use it as an operand in an expression.
Therefore, each of the following expressions is valid:
x = power(y);
if(max(x,y) > 100) printf("greater");
for(ch=getchar(); isdigit(ch); ) ... ;
As a general rule, a function cannot be the target of an assignment. A statement such as
swap(x,y) = 100; /* incorrect statement */
is wrong. The C/C++ compiler will flag it as an error and will not compile a program that
contains it. Sometimes, functions that really don't produce an interesting result return
something anyway. For example, printf() returns the number of characters written. Yet it
would be unusual to find a program that actually checked this. In other words, although all
functions, except those of type void, return values, you don't have to use the return value for
anything. A common question concerning function return values is, "Don't I have to assign
this value to some variable since a value is being returned?" The answer is no. If there is no
assignment specified, the return value is simply discarded. Consider the following program,
which uses the function mul() :
int mul(int a, int b);
6
Object Oriented Programming
Lecture (1)
int main()
{
int x, y, z;
x = 10; y = 20;
z = mul(x, y); /* 1 */
printf("%d", mul(x,y)); /* 2 */
mul(x, y); /* 3 */
return 0; }
int mul(int a, int b)
{
return a*b;
}
In line 1, the return value of mul() is assigned to z. In line 2, the return value is not actually
assigned, but it is used by the printf() function. Finally, in line 3, the return value is lost
because it is neither assigned to another variable nor used as part of an expression.