Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
1 Chapter 9 Scope, Lifetime, and More on Functions.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CMSC 1041 Functions II Functions that return a value.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
CSCI 171 Presentation 6 Functions and Variable Scope.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Chapter 7: User-Defined Functions II
Functions and an Introduction to Recursion
INC 161 , CPE 100 Computer Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions and Structured Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

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 Wed July 8, 2002

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 /* 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 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 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 void myFunc(){ int x = 0; x++; printf("x = %d\n", x); } void main(){ myFunc(); }

Storage Classes auto ; –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 ; –allocated at the beginning of the program execution. –remains allocated until the program terminates.

example #include 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 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 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 ; if a variable has const before its definition, that variable is marked as a constant, and its value cannot be changed. const double PI = ; double E = 2.718; E = PI;/*legal*/ E = PI + 1;/*legal*/ PI = E;/*ILLEGAL */ PI++; PI--;/*ILLEGAL */ E = ++PI;/*ILLEGAL */

Simple Macros: #define #define 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 =

#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 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 % ; printf("Random number between 1 and 100: %d\n", x); }

Find 7 distinct errors... #include #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); }