Вы находитесь на странице: 1из 33

# Analytical and Computational Methods in Civil Engineering I

## 2nd Semester A.Y. 2014-2015

Lecture 4 C Functions
Elvin B. Cruz
Lecturer II
Institute of Civil Engineering

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

Functions

CE 26 Lecture 4 C Functions

Functions
Types of Functions:

## 1. Standard library Functions

2. User-defined functions
Parts:

1. Declaration
2. Definition (body)

CE 26 Lecture 4 C Functions

Function Prototype
(Declaration)
Introduces the name, return type, and
parameters of a function
Must be done before calling the function
int myfunction ( float h, int y);
Return Type
Function
Name
CE 26 Lecture 4 C Functions

Parameters

Function Definition
Contains the executable statements of the function and
all its variable and their declarations

Function
definition of
the main ()
function

CE 26 Lecture 4 C Functions

Example:
#include<stdio.h>
double sqr ( double x );
int main( )
{
:
disc = sqr ( b ) 4*a*c;
:
}
double sqr ( double x )
{
return (x*x) ;
}
CE 26 Lecture 4 C Functions

Recursive Functions

CE 26 Lecture 4 C Functions

Recursive Functions
A recursive function is a function that calls itself
either directly or indirectly through another
function.
The function actually knows how to solve only the
simplest case(s), or so-called base case(s). If the
function is called with a base case, the function
simply returns a result. If the function is called with
a more complex problem, the function divides the
problem into pieces until it arrives with the base
case.
CE 26 Lecture 4 C Functions

CODE:

CE 26 Lecture 4 C Functions

OUTPUT:

CODE:

CE 26 Lecture 4 C Functions

OUTPUT:

Math Functions

CE 26 Lecture 4 C Functions

Math Functions

CE 26 Lecture 4 C Functions

Math Functions

CE 26 Lecture 4 C Functions

CE 26 Lecture 4 C Functions

CE 26 Lecture 4 C Functions

C Identifiers

CE 26 Lecture 4 C Functions

a. File Scope
Such identifiers are known (i.e., accessible)
in all functions from the point at which the
identifier is declared until the end of the file.
Global variables, function definitions, and
function prototypes placed outside the main
function all have file scope.

CE 26 Lecture 4 C Functions

b. Block/Body Scope
Block scope ends at the terminating right brace (}) of the
block.
Local variables defined at the beginning of a function
have 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 the
same name as an identifier in an inner block, the
identifier in the outer block is hidden until the inner
block terminates.
CE 26 Lecture 4 C Functions

## c. Function Scope (Labels)

Labels (an identifier followed by a colon) are
the only identifiers with function scope.
Labels can be used anywhere in the function
in which they appear, but cannot be
referenced outside the function body.
Labels are used in switch statements (as case
labels) and in goto statements

CE 26 Lecture 4 C Functions

Example: goto

CE 26 Lecture 4 C Functions

## d. Function Prototype Scope

The only identifiers with function-prototype scope are those
used 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

## Random Number Generation

CE 26 Lecture 4 C Functions

## Random Number Generation

rand ()
- defined within <stdlib.h>
- rand () generates an integer between 0 and
RAND_MAX (32767)
- If rand () truly produces integers at random,
every number between 0 and RAND_MAX has
an equal chance (or probability) of being
chosen each time rand is called.
CE 26 Lecture 4 C Functions

CE 26 Lecture 4 C Functions

## Random Number Generation

srand(seed);
function defined within <stdlib.h>
function rand actually generates pseudorandom
numbers, hence, the sequence repeats itself each time
the 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 the
program.
The function time() is commonly used as the seed of the
srand() function
CE 26 Lecture 4 C Functions

## Random Number Generation

CODE:

OUTPUT:

CE 26 Lecture 4 C Functions

## C Standard Library Header Files

CE 26 Lecture 4 C Functions

CE 26 Lecture 4 C Functions

CE 26 Lecture 4 C Functions

CE 26 Lecture 4 C Functions

CE 26 Lecture 4 C Functions

References:
C How to Program: Introducing C++ and Java,
3rd Ed. byDeitel&Deitel.
Lecture Notes and Presentations by Dr. Eric
Tingatinga and Rahf Alvarez

CE 26 Lecture 4 C Functions