You are on page 1of 3

FUNCTIONS

also called subroutines, subprograms


 allow complicated programs to be divided into small blocks – “divide and conquer”
 a named sequence of statements that performs some useful procedure
 is a complete and independent program

Functions ◦ also called subroutines, subprograms ◦ allow complicated programs to be parcelled up


into small blocks.
A function must have: 
 Return type (VOID if function does not return anything)
Name followed by a pair of () ◦ Names follow rules in writing identifiers
0 or more parameters to be placed inside the pair of ()
- A parameter is a local variable. It has a data type and a name. ◦ It is a special kind of variable
that refers to data that a subroutine receives on which to operate.
 Pair of curly braces {} (inside the brackets is the function body)
 A return value.

POINTERS
A pointer is a variable that holds a memory address. Because it holds a memory address it is said to
be pointing to the value at that memory location. You can use a pointer in a certain way to get the
value at the address to which it points.
To declare a pointer: put a * in front of its name.
here is an examaple of how to declare a pointer to an integer and an untyped pointer:
int main()

{    
int *p;    
return 0;
}

You can put the address of an integer into a pointer to an integer by using the & operator to get the
integer's address.

int main()

 int i;    
int *p;   
 i = 5;    
p = &i;    
return 0;
}

You can access the value of the integer that is being pointed to by dereferencing the pointer. The * is
used to dereference a pointer. Changing the value pointed to by the integer will change the value of
the integer.

int main()
{    
int i,j;   
int *p;   
i = 5;    
p = &i;    
j = *p;  //j = i    
*p = 7;  //i = 7   
return 0;
}

A variable is declared by giving it a type and a namE (e.g. int k;)


A pointer variable is declared by giving it a type and a name (e.g. int *ptr) where the asterisk
tells the compiler that the variable named ptr is a pointer variable and the type tells the compiler
what type the pointer is to point to (integer in this case).
Once a variable is declared, we can get its address by preceding its name with the unary &
operator, as in &k.
We can "dereference" a pointer, i.e. refer to the value of that which it points to, by using the
unary '*' operator as in *ptr.
An "lvalue" of a variable is the value of its address, i.e. where it is stored in memory. The
"rvalue" of a variable is the value stored in that variable (at that address).

ARRAYS
Arrays are C data types that help us organize large amounts of information.
An array is an ordered list of values

The values held in an array are called array elements


An array stores multiple values of the same type – the element type
The element type can be a primitive type
Therefore, we can create an array of integers, an array of floats, an array of doubles.

The entire array has a single name – Scores


Each value has a numeric index
An array of size N is indexed from zero to N-1
This array holds 10 values that are indexed from 0 to 9

 A particular value in an array is referenced using the array name followed by the index in brackets
 For example, the expression scores[2] refers to the value 94 (the 3rd value in the array)
 That expression represents a place to store a single integer and can be used wherever an integer
variable can be used

Declaring array
It is possible to initialize an array when it is declared:
float prices[3] = {1.0, 2.1, 2.0};
 Or to initialize it later:
int a[6];
a[0]=3;
a[1]=6;
Functions- also called subroutines, subprograms
 allow complicated programs to be divided into small blocks – “divide and conquer”
 a named sequence of statements that performs some useful procedure
 is a complete and independent program

Parameter - A piece of information you provide in order to call a function.


Parameters are like variables in the sense that they contain values and have types

Argument - A value that you provide when you call a function. This value must have the same type as
the corresponding parameter.

You might also like