functions in c · functions call by value there are two ways to invoke functions in many...
TRANSCRIPT
![Page 1: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/1.jpg)
Department of Computer Science
DCS
COMSATS Institute of Information Technology
Functions in C
Rab Nawaz JadoonAssistant Professor
COMSATS IIT, Abbottabad
Pakistan
Introduction to Computer Programming (ICP)
![Page 2: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/2.jpg)
Department of Computer Science
Class Quiz 1
Write a program using function that prints square of first ten digits???
Do it in just 5 minutes?
2
![Page 3: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/3.jpg)
Department of Computer Science
Program
3
![Page 4: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/4.jpg)
Department of Computer Science
Class Quiz 2
Write a program using function that finds the maximum of three given numbers?
Do it in just 5 minutes?
4
![Page 5: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/5.jpg)
Department of Computer Science
Progam
5
![Page 6: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/6.jpg)
Department of Computer Science
Math Library function
6
![Page 7: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/7.jpg)
Department of Computer Science
Math Library function
7
![Page 8: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/8.jpg)
Department of Computer Science
Random number generation
The element of chance can be introduced into computer applications by using the C Standard Library function rand from the <stdlib.h> header.
Consider the following statement:
i=rand()
The rand function generates an integer between 0 and RAND_MAX(a symbolic constant)
Standard C states that the value of RAND_MAX must be at least 32767 (i.e., 16-bit) integer.
8
![Page 9: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/9.jpg)
Department of Computer Science
Sixe sided dice roller
9
![Page 10: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/10.jpg)
Department of Computer Science
Rolling a six sided die 6000 times
10
![Page 11: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/11.jpg)
Department of Computer Science
Cont…
11
![Page 12: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/12.jpg)
Department of Computer Science
Cont…
12
![Page 13: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/13.jpg)
Department of Computer Science
Output
1 987
2 984
3 1029
4 974
5 1004
6 1022
13
![Page 14: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/14.jpg)
Department of Computer Science
Functions call by value
There are two ways to invoke functions in many programming languages
Call-by-value and
Call-by-reference.
When arguments are passed by value, a copy of the argument’s value is made and passed to the called function.
Changes to the copy do not affect an original variable’s value in the caller.
Call-by-value should be used whenever the called function does not need to modify the value of the caller’s original variable.
In C, all calls are by value.
14
![Page 15: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/15.jpg)
Department of Computer Science
Program
15
output
![Page 16: Functions in C · Functions call by value There are two ways to invoke functions in many programming languages Call-by-value and Call-by-reference. When arguments are passed by value,](https://reader036.vdocument.in/reader036/viewer/2022081613/5fbebfa0a667125726573968/html5/thumbnails/16.jpg)
Department of Computer Science
Call by Reference
When an argument is passed by reference, the caller allows the called function to modify the original variable’s value.
Call-by-reference should be used only with trusted called functions that need to modify the original variable.
Call-by reference is used by using address operators and indirection operators.
For example arrays are passed automatically by reference (see later)
16