Академический Документы
Профессиональный Документы
Культура Документы
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. 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
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
CE 26 Lecture 4 C Functions
Example: goto
CE 26 Lecture 4 C Functions
CE 26 Lecture 4 C Functions
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
OUTPUT:
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
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