<< Chapter < Page Chapter >> Page >

Lets look at an example program:

#include<stdio.h>#include<conio.h>int mult ( int x, int y ); int main(){ int x;int y; printf("Please input two numbers to be multiplied: ");scanf("%d%d",&x,&y); printf("The product of your two numbers is %d\n", mult ( x, y )) ;return 0; getch();} int mult ( int x, int y ){ return x * y;}

Parameters passing

The parameters of a function are ordinary local variables. The program creates them, and initializes them with the values of the corresponding arguments, when a function call occurs. Their scope is the function block. A function can change the value of a parameter without affecting the value of the argument in the context of the function call. In the following listing, the factorial( ) function, which computes the factorial of a whole number, modifies its parameter n in the process.

//factorial( ) calculates n!, the factorial of a non-negative number n. // For n>0, n! is the product of all integers from 1 to n inclusive. // 0! equals 1.// Argument: A whole number, with type unsigned int. // Return value: The factorial of the argument, with type long double.long double factorial(register unsigned int n) {long double f = 1; while ( n>1 ) f *= n--;return f; }

Although the factorial of an integer is always an integer, the function uses the type long double in order to accommodate very large results. As the above listing illustrates, you can use the storage class specifier register in declaring function parameters. The register specifier is a request to the compiler to make a variable as quickly accessible as possible. No other storage class specifiers are permitted on function parameters.

Arrays as function parameters

If you need to pass an array as an argument to a function, you would generally declare the corresponding parameter in the following form:

type name[ ]

Because array names are automatically converted to pointers when you use them as function arguments, this statement is equivalent to the declaration:

type *name

When you use the array notation in declaring function parameters, any constant expression between the brackets ([ ]) is ignored. In the function block, the parameter name is a pointer variable, and can be modified. Thus the function addArray() in the following listing modifies its first two parameters as it adds pairs of elements in two arrays.

// addArray( ) adds each element of the second array to the // corresponding element of the first (i.e., "array1 += array2", so to speak).// Arguments: Two arrays of float and their common length. // Return value: None.void addArray( register float a1[ ], register const float a2[ ], int len ) {register float *end = a1 + len; for ( ; a1<end; ++a1, ++a2 ) *a1 += *a2;}

An equivalent definition of the addArray() function, using a different notation for the array parameters, would be:

void addArray( register float *a1, register const float *a2, int len ) { /* Function body as earlier. */ }

An advantage of declaring the parameters with brackets ([ ]) is that human readers immediately recognize that the function treats the arguments as pointers to an array, and not just to an individual float variable. But the array-style notation also has two peculiarities in parameter declarations :

  • In a parameter declaration and only there C allows you to place any of the type qualifiers const, volatile, and restrict inside the square brackets. This ability allows you to declare the parameter as a qualified pointer type.
  • Furthermore, in C you can also place the storage class specifier static, together with a integer constant expression, inside the square brackets. This approach indicates that the number of elements in the array at the time of the function call must be at least equal to the value of the constant expression.

Here is an example that combines both of these possibilities:

int func( long array[const static 5] ){ /* ... */ }

In the function defined here, the parameter array is a constant pointer to long, and so cannot be modified. It points to the first of at least five array elements.

In the following listing, the maximum( ) function's third parameter is a two-dimensional array of variable dimensions.

// The function maximum( ) obtains the greatest value in a // two-dimensional matrix of double values. // Arguments: The number of rows, the number of columns, and the matrix.// Return value: The value of the greatest element. double maximum( int nrows, int ncols, double matrix[nrows][ncols] ){ double max = matrix[0][0];for ( int r = 0; r<nrows; ++r ) for ( int c = 0; c<ncols; ++c ) if ( max<matrix[r][c]) max = matrix[r][c];return max; }

The parameter matrix is a pointer to an array with ncols elements.

Pointers as function parameters

Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function. For instance, a sorting routine might exchange two out-of-order arguments with a function called swap. It is not enough to write

swap(a, b);

where the swap function is defined as

void swap(int x, int y) /* WRONG */ {int temp; temp = x;x = y; y = temp;}

Because of call by value, swap can't affect the arguments a and b in the routine that called it. The function above swaps copies of a and b.

The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed:


Since the operator&produces the address of a variable,&a is a pointer to a. In swap itself, the parameters are declared as pointers, and the operands are accessed indirectly through them.

void swap(int *px, int *py) /* interchange *px and *py */ {int temp; temp = *px;*px = *py; *py = temp;}


Pictorially in [link]

swap function with pointer parameters

Questions & Answers

What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Berger describes sociologists as concerned with
Mueller Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?