220 likes | 325 Views
Wed July 8, 2002. Lecture 05 Functions II, Storage Class, Scope, rand(). METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan. Today. Functions... Storage Classes : auto, static Variable Scope Recursion const, #define
E N D
Wed July 8, 2002 Lecture 05Functions II, Storage Class, Scope,rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan
Today • Functions... • Storage Classes : auto, static • Variable Scope • Recursion • const, #define • Random numbers: rand()
Why 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.
Function 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.)
float mySquare(float) #include <stdio.h> /* Function definition */ float mySquare(float x) { float y; y = x*x; 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) ); }
float mySquare(float) #include <stdio.h> 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 = x*x; return y; }
Multiple input parameters • Functions can take any number of parameters; separated by commas. #include <stdio.h> 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; }
what's the output? #include <stdio.h> void myFunc(){ int x = 0; x++; printf("x = %d\n", x); } void main(){ myFunc(); myFunc(); myFunc(); }
Storage Classes • auto <varType> <varName> ; • 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 <varType> <varName> ; • allocated at the beginning of the program execution. • remains allocated until the program terminates.
example #include <stdio.h> void main(){ int i; void Arttir(void); for(i=0; i<5; i++) Arttir(); } void Arttir(void){ auto int autoX=0; /*equivalent to: "int autoX;" */ static int statX=0; autoX++; statX++; printf("auto = %d \t static = %d\n", autoX, statX); }
Variable 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.
Global 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.
#include <stdio.h> int x;/* accessible in Input(), Square(), Output(), main() */ void Input(){ scanf("%d", &x); } int y; /* accesible in Square(), Output(), main() */ void Square() { y = x*x; } void Output() { printf("%d squared = %d\n", x, y); } void main() { Input(); printf("main: read x = %d\n", x); Square(); printf("main: calculated x^2 = %d", y); Output(); printf("main: called Output..\n"); }
Identical 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.
#include <stdio.h> 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); sayi = 7; MyFonksiyon(); printf("main: sayi = %d\n", sayi); }
Recursion • 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); }
Type Qualifier: const const <varType> <varName>; • 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 */
Simple Macros: #define #define <macroName> <sequence-of-tokens> • 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 <varType> <macroName> = <sequence-of-tokens>
#define example #define PI 3.14 #define NUM_EXAMS 3 float SphereSurface(float radius){ return 4 * PI * radius * radius; }
Random 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
rand() example #include <stdio.h> #include <stdlib.h> #include <time.h> 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); }
Find 7 distinct errors... #include <stdio.h> #define PASS_LOW 60; #define PASS_TOP 100; int 1stFunc(int n){ int num1, _, su#m; printf("Please enter grades for %d exams : "); for(num1=0, num1<n, num1++){ scanf("%d", _); su#m += _; } return su#m; } int main() { double total, avg; avg = 1stFunc( 3 ) / 3; if ( PASS_LOW <= avg <= PASS_TOP) printf("you pass\n"); else printf("sorry, you fail.\n"); exit(2); printf("your average is: %f\n", total); }