![Page 1: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/1.jpg)
Ahmed Hassan DallalTutorial: 3Feb 2011
![Page 2: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/2.jpg)
Acknowledgement Parts of this presentation are captured from Eng. Islam
Badreldin tutorials on Data Structures and Algorithms in C.
![Page 3: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/3.jpg)
![Page 4: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/4.jpg)
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
![Page 5: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/5.jpg)
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.
![Page 6: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/6.jpg)
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.
![Page 7: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/7.jpg)
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)
![Page 8: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/8.jpg)
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.
![Page 9: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/9.jpg)
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));}
![Page 10: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/10.jpg)
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).
![Page 11: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/11.jpg)
Function call by value
What is the expected output ?!
![Page 12: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/12.jpg)
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!
![Page 13: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/13.jpg)
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.
![Page 14: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/14.jpg)
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 );
![Page 15: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/15.jpg)
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.
![Page 16: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/16.jpg)
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.
![Page 17: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/17.jpg)
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.
![Page 18: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/18.jpg)
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.
![Page 19: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/19.jpg)
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.
![Page 20: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/20.jpg)
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.
![Page 21: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/21.jpg)
Variable scope and lifetime Global Variable:
![Page 22: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/22.jpg)
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.
![Page 23: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/23.jpg)
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.
![Page 24: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/24.jpg)
![Page 25: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/25.jpg)
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.
![Page 26: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/26.jpg)
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.
![Page 27: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/27.jpg)
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];
![Page 28: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/28.jpg)
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.
![Page 29: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/29.jpg)
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]
![Page 30: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/30.jpg)
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.
![Page 31: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/31.jpg)
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.
![Page 32: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/32.jpg)
Assignment Write a program that asks the user to enter 7 numbers,
store them into an array, and displays the sum of array elements.
![Page 33: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/33.jpg)
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.
![Page 34: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/34.jpg)
Declaring a 2-D Array
Example: int a[3][4];
![Page 35: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/35.jpg)
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.
![Page 36: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/36.jpg)
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.
![Page 37: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/37.jpg)
Assignment
Write a program that asks the user to enter two 2x2 matrices, add them ,and displays the result.
![Page 38: Ahmed Hassan Dallal - unimasr.net · 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](https://reader031.vdocuments.us/reader031/viewer/2022030711/5af9f9c47f8b9a19548d58c2/html5/thumbnails/38.jpg)
Thank you