lecture 4 - c functions
TRANSCRIPT
-
8/11/2019 Lecture 4 - C Functions
1/33
Analytical and Computational Methods in Civil Engineering I1 st Semester A.Y. 2013-2014
Lecture 4 C Functions
Elvin B. CruzLecturer II
Institute of Civil Engineering
-
8/11/2019 Lecture 4 - C Functions
2/33
Overview:- Functions in C Programming- Recursive Functions
- Math Functions- Scope and Lifetime of C Identifiers- Random Number Generation in C
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
3/33
Functions
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
4/33
Functions
Types of Functions:
1. Standard library Functions
2. User-defined functions
Components:
1. Declaration2. Definition (body)
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
5/33
Function Declaration
Introduces the name, return type, andparameters of a function
Must be done before calling the function
int myfunction ( float h, int y);
CE 26 Lecture 4 C Functions
Return Type
FunctionName
Parameters
-
8/11/2019 Lecture 4 - C Functions
6/33
Function Definition
CE 26 Lecture 4 C Functions
Contains the executable statements of the function and
all its variable and their declarations
Functiondefinition ofthe main ()function
-
8/11/2019 Lecture 4 - C Functions
7/33
#include
double r ( double x );
int main( ){
:disc = sqr ( b ) 4*a*c;
: }
double r ( double x ){
return (x*x) ;}
Example:
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
8/33
Recursive Functions
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
9/33
Recursive Functions
A recursive function is a function that calls itselfeither directly or indirectly through anotherfunction.
The function actually knows how to solve only thesimplest case(s), or so-called base case(s) . If thefunction is called with a base case, the functionsimply returns a result. If the function is called witha more complex problem, the function divides theproblem into pieces until it arrives with the basecase.
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
10/33
CE 26 Lecture 4 C Functions
CODE: OUTPUT:
-
8/11/2019 Lecture 4 - C Functions
11/33
CE 26 Lecture 4 C Functions
CODE: OUTPUT:
-
8/11/2019 Lecture 4 - C Functions
12/33
Math Functions
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
13/33
CE 26 Lecture 4 C Functions
Math Functions
-
8/11/2019 Lecture 4 - C Functions
14/33
CE 26 Lecture 4 C Functions
Math Functions
-
8/11/2019 Lecture 4 - C Functions
15/33
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
16/33
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
17/33
Scope and Lifetime ofC Identifiers
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
18/33
a. File Scope
Such identifiers are known (i.e., accessible)in all functions from the point at which theidentifier is declared until the end of the file.
Global variables, function definitions, andfunction prototypes placed outside the mainfunction all have file scope.
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
19/33
b. Block/Body Scope
Block scope ends at the terminating right brace (}) of theblock.
Local variables defined at the beginning of a functionhave block scope as do function parameters (inputs) ,which are considered local variables by the function.
Any block may contain variable definitions. When blocks
are nested, and an identifier in an outer block has thesame name as an identifier in an inner block, theidentifier in the outer block is hidden until the innerblock terminates.
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
20/33
c. Function Scope (Labels)
Labels (an identifier followed by a colon suchas start: or stop:) are the only identifiers withfunction scope.
Labels can be used anywhere in the functionin which they appear, but cannot bereferenced outside the function body.
Labels are used in switch statements (as caselabels) and in goto statements
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
21/33
Example: goto
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
22/33
d. Function Prototype Scope
The only identifiers with function-prototype scope are thoseused in the parameter list of a function prototype.
Example:
void function(int x); // prototype -- x is a parameter
.
void function (int x) // declaration -- x is a parameter{Body statement }
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
23/33
Random Number Generation
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
24/33
Random Number Generation
rand ()- defined within
- rand () generates an integer between 0 andRAND_MAX(32767)
- If rand () truly produces integers at random,every number between 0 and RAND_MAXhasan equal chance (or probability) of beingchosen each time rand is called.
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
25/33
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
26/33
Random Number Generation
srand( seed ); function defined within
function rand actually generates pseudorandomnumbers, hence, the sequence repeats itself each timethe program is executed
function srand takes an unsigned integer argument(seed) and seeds function rand to produce a different
sequence of random numbers for each execution of theprogram.
The function time() is commonly used as the seed of thesrand() function
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
27/33
Random Number Generation
CE 26 Lecture 4 C Functions
CODE:
OUTPUT:
-
8/11/2019 Lecture 4 - C Functions
28/33
C Standard Library Header Files
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
29/33
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
30/33
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
31/33
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
32/33
CE 26 Lecture 4 C Functions
-
8/11/2019 Lecture 4 - C Functions
33/33
References:
C How to Program: Introducing C++ and Java,3 rd Ed. byDeitel&Deitel.
Lecture Notes and Presentations by Dr. EricTingatinga and Rahf Alvarez
CE 26 Lecture 4 C Functions