CS 111 Computing Fundamentals: Functions (2) Dr. Christina Class

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

CS 111 Computing

Fundamentals

Functions (2)
Dr. Christina Class
Repetition
Functions: Variables
Local variables
 Variables that are defined within a function
a local variables
 They are administered automatically by the
runtime system.
 Their initial value is undefined.
 They can only be accessed from within the
function that defined it.
 They will be removed from the memory
when the function ends.
 Remark for SIC students: They are stored on
the stack.
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 4
 Sharing of information between a
function A and the function B that
called the function A:
 from the caller to the function: using
parameters
 from the function to the caller: using
the (only one) return value
 Functions can also share
information by using global
variables.
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 5
Global variables

 Global variables are defined


outside every function.
 They can be accessed from within
all code segments after their
declaration.
 They are initialized with 0.
 They exist as long as the program
is running.
 Remark for SIC Students: They are
stored
CS 116 on -the
– Summer 2014/15 heap.
Dr. Christina Class - Lecture 11 6
Variables

local (automatic) global


declaration within a function (or outside any function
block)
initial value undefined 0
accessibility from within the function / from the whole file (if
the block not hidden)
lifetime of the function / the block of the whole program

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 7


Examples
#include <stdio.h>

int a;
global variable
void read(void)
{
scanf("%d",&a);
}

void write(void)
{
printf("a = %d\n", a);
}

int main()
{
read();
write();
return 0;
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 8


#include <stdio.h>

int a;

void read()
{
scanf("%d",&a);
}

void write()
{
printf("a = %d\n", a);
}

int main()
{
write();
read();
write();
return 0;
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 9


#include <stdio.h>
global variable
int a=4;
local variable,
void write() hides global variable
{ with the same name
int a=13;
printf("a = %d\n", a);
}
access to local variable
int main()
{
write();
printf("a = %d\n", a);
return 0; access to global variable
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 10
 With each call to a function, its
local variables are automatically
recreated

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 11


#include <stdio.h>

void myFunction(void)
{
int value=1;
printf("The value is: %d.\n", value);
value++;
}

int main()
{
myFunction();
myFunction();
myFunction();
return 0;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 12
static variables

 A static variable is a local variable


which is not deleted after the end
of a function.
 Its lifetime is as long as the
program exists.
 It is initialized with zero.
 Its access is limited to the function
that defines it.

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 13


#include <stdio.h>

void myFunction(void)
{
static int value=1;
printf("The value is: %d.\n", value);
value++;
}

int main()
{
myFunction();
myFunction();
myFunction();
return 0;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 14
Variables

local static local global


(automatic)
declaration within a within a function outside any
function (or (or block) function
block) using the
keyword static
initial value undefined 0 0
accessibility from within the from within the from the whole
function / function / block file (if not
block hidden)
lifetime of the of the whole of the whole
function / the program program
block

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 15


Functions: Parameter
 Parameters of the standard types (int,
char, float, etc.) in C are call-by-value
parameters.
 When the function is called, the value
of the actual parameter (in the
function call) is copied and passed to
the function parameter.
 The function works with a copy!
 This allows for passing literal constants
as parameters. (e.g. squares(1,5);)
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 17
#include <stdio.h>

this is a copy of a value


void toSquare(int a)
{
a = a*a;
printf("a in toSquare: %d\n",a);
}

int main()
{
int a = 15;
toSquare(a);
printf("a in main: %d\n",a);
return 0;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 18
 If we wish to change the value in
the calling code segment, we need
to use a return value and assign it
to the variable.

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 19


#include <stdio.h>

int toSquare(int a)
{
a = a*a;
printf("a in toSquare: %d\n",a);
return a;
}

int main()
{
int a = 15;
a = toSquare(a);
printf("a in main: %d\n",a);
return 0;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 20
Arrays as parameters
 Arrays can be used as parameters but not
returned by a function.
 Arrays are call-by-reference
parameters, i.e. the function and the
caller share the same array copy and all
changes are permanent.
 The length in the parameter definition of
an array is ignored.
 When the function is called, only the
name of the array is passed as parameter.

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 22


#include <stdio.h>
int main()
void read(int array[]) {
{
int i; int array[5];
for (i = 0; i < 5; i++)
scanf("%d",&array[i]); read(array);
}
write(array);
void write(int array[])
{
return 0;
int i; }
for (i = 0; i < 5; i++)
printf("%d ",
array[i]);
printf("\n");
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 23


void write(int array[], int length)
{
int i;
for (i = 0; i < length; i++)
printf("%d ", array[i]);
printf("\n");
}
int main()
{
int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {11,12,13,14};
write(array1,10);
write(array2,4);
write(array1,4);
return 0;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 24
 If we use a two dimensional array
as a parameter, the column
dimension must be specified.
 e.g.
void readMatrix(int matrix[][3], int
rows)
{
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 25


Exercises

 Study the handout.


 Implement a function to return the
minimum of the array.
 Implement a function to print the
array values.

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 11 26

You might also like