Title: Lecture 05 Functions II, Storage Class, Scope, rand()
1Lecture 05Functions II, Storage Class,
Scope,rand()
Wed July 8, 2002
- METU Dept. of Computer Eng. Summer 2002
- Ceng230 - Section 01
- Introduction To C Programming
- by Ahmet Sacan
2Today
- Functions...
- Storage Classes auto, static
- Variable Scope
- Recursion
- const, define
- Random numbers rand()
3Why Functions?
- Code Design
- breaks up programs into little pieces.
- thus, makes implementation easier.
- Code Reuse
- no need to re-invent the wheel every time you
wanted to use a wheel. - Once implemented, a function can be used multiple
times in the same program. - Functions can be bundled into libraries and you
can use it in different programs.
4Function Declaration (Prototyping) reminder
- Before you can use a function, you must give the
compiler knowledge about what parameters the
function accepts, and what type it returns. - If you have already written the definition (the
body of the function) before the call, then the
prototyping is optional. - (See previous lecture-notes for different
acceptable versions.)
5float mySquare(float)
- include ltstdio.hgt
- / Function definition /
- float mySquare(float x)
- float y y xx return y
-
- void main ()
- int z 12, result
- printf("5 squared f\n", mySquare(5) )
- printf("1.98 squared f\n", mySquare(1.98) )
- result mySquare(z)
- printf("d squared d\n", (int) mySquare(z) )
6float mySquare(float)
- include ltstdio.hgt
- float mySquare(float x) / function
Declaration / - void main ()
- int z 12, result
- printf("5 squared f\n", mySquare(5) )
- printf("1.98 squared f\n", mySquare(1.98) )
- result mySquare(z)
- printf("d squared d\n", (int) mySquare(z) )
-
- / Function definition /
- float mySquare(float x)
- float y y xx return y
-
7Multiple input parameters
- Functions can take any number of parameters
separated by commas. - include ltstdio.hgt
- float RectArea ( float length , float width )
- return length width
-
- int main ()
- float x, y
- printf ("please enter Length , width " )
- scanf ("f , f", x, y)
- printf (" Area f\n", RectArea (x, y))
- return 0
-
8what's the output?
- include ltstdio.hgt
- void myFunc()
- int x 0
- x
- printf("x d\n", x)
-
- void main()
- myFunc()
- myFunc()
- myFunc()
9Storage Classes
- auto ltvarTypegt ltvarNamegt
- allocated upon entry to a segment of code
- de-allocated upon exit from this segment.
- By default, the variables you declare inside a
function are auto. - static ltvarTypegt ltvarNamegt
- allocated at the beginning of the program
execution. - remains allocated until the program terminates.
10example
- include ltstdio.hgt
- void main()
- int i
- void Arttir(void)
-
- for(i0 ilt5 i) Arttir()
-
- void Arttir(void)
- auto int autoX0 /equivalent to "int autoX"
/ - static int statX0
- autoX statX
- printf("auto d \t static d\n", autoX,
statX)
11Variable Scope (Visibility)
- The part of the program in which a name can be
used is called its scope. - Variables declared inside a block ... of code
are not visible outside of the block. - Because each function has its own block of code,
you can declare many variables with the same name
as long as they are in different functions. Even
though these variables may have the same names,
they are completely independent.
12Global Variables
- Local variables
- can only be accessed by the functions in which
they are defined. They are unknown to the other
functions in the same program. - i.e., the scope of a local variable is just the
function it's defined in. - Global variables
- Defined outside the functions.
- the scope is the rest of the source file starting
from its definition.
13- include ltstdio.hgt
- int x / accessible in Input(), Square(),
Output(), main() / - void Input() scanf("d", x)
- int y / accesible in Square(), Output(), main()
/ - void Square() y xx
- void Output() printf("d squared d\n", x,
y) - void main()
- Input()
- printf("main read x d\n", x)
- Square()
- printf("main calculated x2 d", y)
- Output()
- printf("main called Output..\n")
14Identical Names for local global ?
- A local variable definition supersedes that of a
global variable. - If a global variable and a local variable have
the same names, all references to that name
inside the function will refer to the local
variable.
15- include ltstdio.hgt
- int sayi
- void FonksiyonumBenim()
- int sayi 3
- sayi
- printf("FonksiyonumBenim sayi d\n", sayi)
-
- void MyFonksiyon()
- sayi
- printf("MyFonksiyon sayi d\n", sayi)
-
- void main()
- sayi 5
- FonksiyonumBenim()
- printf("main sayi d\n", sayi)
16Recursion
- Functions can call themselves...
- void myFunc(int count)
- printf("count d\n", count)
- if(!count)
- return
- / else /
- myFunc(count-1)
- printf("X")
-
- void main()
- myFunc(5)
17Type Qualifier const
- const ltvarTypegt ltvarNamegt
- if a variable has const before its definition,
that variable is marked as a constant, and its
value cannot be changed. - const double PI 3.1415
- double E 2.718
- E PI /legal/
- E PI 1 /legal/
- PI E /ILLEGAL /
- PI PI-- /ILLEGAL /
- E PI /ILLEGAL /
18Simple Macros define
- define ltmacroNamegt ltsequence-of-tokensgt
- NO SEMICOLON!! at the end of define directive.
- A define directive can appear anywhere in the
program but usually, the are collected together
at the beginning of the program. - Using symbolic names for constants helps make
programs extendible and portable. - We will only use define directives to set
constant values. So, for our purposes, you may
consider the above define statement as
equivalent to the following - const ltvarTypegt ltmacroNamegt ltsequence-of-tokensgt
19define example
- define PI 3.14
- define NUM_EXAMS 3
- float SphereSurface(float radius)
- return 4 PI radius radius
20Random Numbers
- You can use rand( ) function defined in the
stdlib.h header file. - The random seed can be set using the srand
function. - A good seed is the time, which can be obtained
using the time function defined in the time.h
library
21rand() example
- include ltstdio.hgt
- include ltstdlib.hgt
- include lttime.hgt
- void main ()
- int n, x
- unsigned int t time(0)
- printf("Time d\n", t) / Print the
computer's time / - / Set the random number generator with a seed
/ - srand(t)
- n rand()
- printf("Random number between 0 and d d\n",
RAND_MAX, n) -
- / Convert the number to the range 1 to 100 /
- x n 100 1
- printf("Random number between 1 and 100 d\n",
x)
22Find 7 distinct errors...
include ltstdio.hgt define PASS_LOW 60 define
PASS_TOP 100 int 1stFunc(int n) int num1, _,
sum printf("Please enter grades for d exams
") for(num10, num1ltn, num1) scanf("d",
_) sum _ return sum int main()
double total, avg avg 1stFunc( 3 ) /
3 if ( PASS_LOW lt avg lt PASS_TOP) printf("y
ou pass\n") else printf("sorry, you
fail.\n") exit(2) printf("your average is
f\n", total)