Upload
vutram
View
215
Download
1
Embed Size (px)
Citation preview
Ahmed Hassan DallalTutorial: 3Feb 2011
Acknowledgement Parts of this presentation are captured from Eng. Islam
Badreldin tutorials on Data Structures and Algorithms in C.
Introduction The best way to develop and maintain a large program
is to construct it from small, simple pieces, or components.
Functions allow the programmer to modularize a program by separating its tasks into self-contained units.
Functions were originally invented to avoid repeating the same code over and over again
Introduction Think about the function as if you assign a job to
another person of your team, you just give him an input and get from him the output after he finished the job.
Remember, you have been using the printf function a lot without even knowing what is the exact code written in it, to you printf is just a person who does a job for you.
Introduction A function is invoked by a function call.
When the called function completes its task, it either
returns a result, or simply
returns control to the caller.
For example “math.h” library provides a lot of globalmathematical function that you can use.
Structure of a function
The above structure of the C function is also known as the function definition.
A function can only return one single value using the return statement, it can also return nothing (void)
Structure of a function The main() is a function too, but a special function
where your program begins execution, and in which you get to call other functions.
Structure of a function To use any function, we need to do a function call in
the main function.
Example:
The function call causes the control to be transferred to the sum function in order to execute the sum function body.
int main(){printf(“%d”, sum(5,7));}
Structure of a function A function terminates execution and returns to the
main function to execute statements after the function call, either by:
encountering a return statement or
reaching the end of the function(the closing brace).
Function call by value
What is the expected output ?!
Function call by value In the previous example, after the function execution,
the value of the main() function variables x and y are not changed even that we changed them in the sum() function, why is that?
This is because function calls are of the call by value type.
Memory allocations :: automatic storage class!
Function Prototypes A function prototype tells the compiler: the name of a function,
the type of data returned by the function,
the number of parameters the function expects to receive,
the types of those parameters, and
the order in which the parameters of those types are expected.
Hence you can declare the function after the main() or anywhere as long as you will place the function prototype before you use it.
Function Prototypes The function prototype is a copy of the function
header terminated by a semicolon.
Notice that we can remove the input variable names from the function prototype!
void maximum( int, int, int );
Function Prototypes Compilation errors occur if the function prototype,
function header and function calls do not all agree in the number, type and order of arguments and parameters, and in the return type.
Assignment Write a function the returns the maximum of three
numbers.
Write a function that returns the factorial of a number.
Write a function to display a welcome message on the screen.
Variable scope and lifetime So far, we defined all the variables inside functions, are
these variables seen or known outside these functions?
Try out this code and notice the value of the variable diff.
Variable scope and lifetime The output of the previous program illustrates that the
variable diff in the main function is not the variable diff in the sumdiff function!!
The portion of the program where an identifier can be used is known as its scope.
Each variable has a certain scope where it is recognized in.
Variable scope and lifetime There are 3 types of variables: Local variables
Global variables
Static variables
Local Variables (automatic variables): The local variables are only visible and accessible in the
function they are defined in.
Local variables are created when the function they are defined in is called, and they are destroyed when this function returns.
Variable scope and lifetime Global Variables: A variable known to all functions in a program rather
than just one, hence it should be declared outside all the functions.
Global variables are destroyed with the program exit.
They are not desirable since: Global variables are not protected from alterations by other
functions that have no business modifying them.
Global variables use memory less efficiently than local variables.
As rule of thumb, variables should be local unless there is a good reason to make them global.
Variable scope and lifetime Global Variable:
Variable scope and lifetime Static variables: In some special situations you will find yourself in need
to define a variable that is local to a function, that is only accessible in one function but is not destroyed when the function returns like in case of the local variable.
In this case, you will need to define a static variable, which retains its values between function calls.
Local variables declared static still have block scope, even though they exist from the time the program begins execution.
Variable scope and lifetime
All numeric variables of the static storage class are initialized to zero if they are not explicitly initialized by the programmer.
Introduction Arrays are data structures consisting of related data
items of the same type.
Sometimes we want to be able to refer to a number of similar data elements collectively.
It will not be convenient to have a unique variable name for each element.
The Arrays are useful in such situations.
Introduction Arrays are collection of variables of a certain type that
are placed contiguously in memory.
In order to define an array, like a variable, the compiler needs to know the data type of the elements contained in the array and the size of the array.
Declaring Arrays Let us define an array for the students marks,
The array definition involves the array elements data type (float), the array name (marks), and the array size enclosed in squared brackets (100).
The addresses of all array elements are consecutive.
float marks[100];
Array initialization We can initialize the array as follows:
Also we can omit the array size from the initialization, the compiler will simply count the number of elements you placed in the array and use it as the array size:
For loops may be used to initialize all the elements.
Accessing array elements We can access individual array elements through
indexing.
The first element index in C is zero, so to access element number zero of an array we use the statement: array_name[0].
The last element index in C is size of array-1, so to access the last element of an array we use the statement: array_name[size of array-1]
Reading and writing data to an array Try out this example, we will place values from 0 to 10
in the array and then print them out on the screen.
Warning you should always make sure you are not accessing an
element outside of the array bounds, the compiler will not warn you and you will get wrong results, or the program may crash!
Array name is equivalent to the address of the firstarray element, so use indexing to access different array elements.
Assignment Write a program that asks the user to enter 7 numbers,
store them into an array, and displays the sum of array elements.
Multidimensional Arrays Multidimensional arrays with two dimensions are
often used to represent tables of values consisting of information arranged in rows and columns.
Arrays that require two subscripts to identify a particular element are called two-dimensional arrays or 2-D arrays.
In general, an array with m rows and n columns is called an m-by-n array.
Declaring a 2-D Array
Example: int a[3][4];
Initializing a 2-D array The values are grouped by row in braces.
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; 1 and 2 initialize b[ 0 ][ 0 ] and b[ 0 ][ 1 ], and 4 initialize b[ 1 ][ 0 ] and b[ 1 ][ 1 ].
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; // initializes: b[ 0 ][ 0 ] to 1, b[ 0 ][ 1 ] to 0, b[ 1 ][ 0 ] to 3, and b[ 1 ][ 1 ] to 4.
Reading and writing data to an array 2-D arrays are accessed by the indices of each row and
column.
Reading and writing may be done using 2 for loops
For each row
For each column
Access a[i][j] cell value.
Assignment
Write a program that asks the user to enter two 2x2 matrices, add them ,and displays the result.
Thank you