C Programming - Lecture VI

Dr. Sorin Draghici
Wayne State University

Summary

The ? : operator

Functions

  • function declaration, definition and call
    The ? : conditional operator
  • function arguments and the return value
  • formal arguments, actual arguments
  • no arguments vs. unspecified arguments
  • call by value
  • altering values in a function: & and *


The ? : conditional operator

if ( x > 0 ) else y = ( x>0) ? x : -x ;


General form: expression1 ? expression2 : expression3

If expression1 evaluates at a true value (non-zero), expression2 will be evaluated and its value will be the value of the whole expression. If expression1 evaluates at a false value (zero), expression3 will be evaluated and its value will be the value of the whole expression.

Example: y = (a>b) ? a : b ;
What does this do ?


Functions


Function declaration, definition and call

int sum( int x, int y ) ; /* this is the declaration (or prototype) of a function sum */

int sum( int x, int y )/* this is the definition of the function sum*/
{
int result ;
result = x + y ;
return( result ) ;
}

Declaration,Definition, Call

void main( void ) /* this is the definition of the function main*/
{
int a = 3 , b = 5 , c ;
c = sum( a, b ) ; /* this is the call of the function sum */
printf("the sum of %d and %d is %d\n", a, b, c ) ;
}

The declaration specifies the name, the parameters (and their types) and the return value. It does not specify what the function does.

The definition specifies everything about the function (name, parameters with their types, return value and what the function does).

When we want to use a function, we 'call' it using its name. The parameters given when we call the function is what the function will use in its computation.


Formal vs. actual arguments


Formal arguments are just place holders. They reserve space for the actual arguments.

Actual arguments are the actual values we want to process. The compiler checks if the actual arguments are indeed of the type declared for the formal arguments.

No arguments vs. unspecified arguments

Declaring a function like this:

int myfunc ( void )

means that the function takes no arguments

Declaring a function like this:

int myfunc ( )

means that we don't know the arguments

The difference is similar to the difference between saying "I don't know what it is in that bag" and "That bag is empty" (i.e. I do know that the bag contains nothing).

Another example

void swap ( double x, double y )
{
x = y ;
y = x ;
}

void swap (double x, double y)
{ double temp ;
temp = x ;
x = y ;
y = temp ;
}


Swapping...

#include
void swap (double x, double y ) ;

void main ( void )
{
double a = 3. , b = 4. ;
printf("a = %lf, b = %lf \n", a, b ) ;
swap( a, b) ;
printf("a = %lf, b = %lf \n", a, b ) ;
}

Let us trace the execution of this program.


Pointers

A pointer is a variable which contains an address of an object (variable, structure, array, etc.)

As any other variable, a pointer has:

  • a type (always an address but to different objects)
  • a name
  • a value (the actual address of the object pointed to)
  • an address (where in the memory the pointer is stored)
Pointers

#include

void swap (double *x, double *y ) ;

void swap (double *x, double *y)
{ double temp ;
temp = *x ;
*x = *y ;
*y = temp ;
}

void main ( void )
{
double a = 3. , b = 4. ;
printf("a = %lf, b = %lf \n", a, b ) ;
swap( &a, &b) ;
printf("a = %lf, b = %lf \n", a, b ) ;
}

Summary


The ? : operator
Functions

  • function declaration, definition and call
  • function arguments and the return value
  • formal arguments, actual arguments
  • no arguments vs. unspecified arguments
  • call by value
  • altering values in a function: & and *
Reading

Chap. 9 (pag. 300 - 317)
Chap. 10 (pag. 320 - 357)