Introduction to Computing Lecture 09: Functions (Part II)

Slides:



Advertisements
Similar presentations
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Advertisements

1 Functions (Bag 2). 2 Topik Scope Prototypes 3 Scope Variabel yg dideklarasikan dalam badan fungsi (termasuk formal parameter) yang hanya dpt diakses.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CS 201 Functions Debzani Deb.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science 210 Computer Organization Introduction to C.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Chapter 7: Function.
Computer Science 210 Computer Organization
Chapter 7: User-Defined Functions II
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Functions Dr. Sajib Datta
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Computer Science 210 Computer Organization
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
The while Looping Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Functions I Creating a programming with small logical units of code.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Functions, Part 1 of 3 Topics Using Predefined Functions
The while Looping Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 7: User-Defined Functions II
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
EPSII 59:006 Spring 2004.
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Introduction to Computing Lecture 08: Functions (Part I)
ETE 132 Lecture 6 By Munirul Haque.
Dale Roberts, Lecturer IUPUI
Lec 6 Loop Statements Introduction to Computer Programming
The while Looping Structure
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions I Creating a programming with small logical units of code.
Presentation transcript:

Introduction to Computing Lecture 09: Functions (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr

Topics Structured Programming Scope Prototypes C Library Functions

Structured Programming Individual program tasks are performed by independent sections of program code Advantages Easier to write, because complex programming problems are broken into a number of smaller, simpler tasks. Each task is performed by a function in which code and variables are isolated from the rest of the program

Structured Programming Advantages It's easier to debug a structured program. If your program has a bug (something that causes it to work improperly), a structured design makes it easy to isolate the problem to a specific section of code (a specific function).

Structured Programming Advantages Time saving: You can easily use a function you wrote in another program that needs to do the same task. Even if the new program needs to do a slightly different task, you can modify the function, that is easier than writing a new one from scratch: Consider the two functions printf() and scanf(). If your functions have been created to perform a single task, using them in other programs is much easier

Planning a Structured Program A program to manage name and address list Enter new names and addresses Modify existing entries Sort entries by last name Print mailing labels

Planning a Structured Program Enter new names and addresses Read the existing address list from disk Prompt the user for one or more new entries Add the new data to the list Save the updated list to disk Modify existing entries Modify one or more entries.

Structured Programming “Do”s and “Don’t”s DO plan before starting to code. By determining your program's structure ahead of time, you can save time writing the code and debugging it DON'T try to do everything in one function. A single function should perform a single task, such as reading information from a file

main() main() is also a function When you return a value in main, it is passed to the operating system 0 indicates that the program terminated normally All other values indicate some error It is a good idea and it is required in ANSI C to return a value in main

Scope Variables declared in a function body (including formal parameters) are only accessible whilst the function is executing In fact, this is true of any block in a program

Example: scope.c int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; globalVar += localVar; return (paramVar + localVar); } int main() int localVar = 5; for (localVar=0; localVar < 5; localVar++) int blockVar = 1; blockVar = increment(blockVar); printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! Out of scope. */ return 0;

Global variable declaration Example: scope.c Global variable declaration int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; globalVar += localVar; return (paramVar + localVar); } int main() int localVar = 5; for (localVar=0; localVar < 5; localVar++) int blockVar = 1; blockVar = increment(blockVar); printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! Out of scope. */ return 0;

Variables local to this block. Example: scope.c int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; globalVar += localVar; return (paramVar + localVar); } int main() int localVar = 5; for (localVar=0; localVar < 5; localVar++) int blockVar = 1; blockVar = increment(blockVar); printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! Out of scope. */ return 0; Variables local to this block.

Scope of the variable localVar. Example: scope.c int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; globalVar += localVar; return (paramVar + localVar); } int main() int localVar = 5; for (localVar=0; localVar < 5; localVar++) int blockVar = 1; blockVar = increment(blockVar); printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! Out of scope. */ return 0; Scope of the variable localVar.

Scope of the variable blockVar. Example: scope.c int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; globalVar += localVar; return (paramVar + localVar); } int main() int localVar = 5; for (localVar=0; localVar < 5; localVar++) int blockVar = 1; blockVar = increment(blockVar); printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! Out of scope. */ return 0; Scope of the variable blockVar.

Variable Scope – “Do”s and “Don’t”s DO use local variables for items such as loop counters DON'T use global variables if they aren't needed by a majority of the program's functions DO use local variables to isolate the values the variables contain from the rest of the program

Prototyping of Functions Must declare functions before use (like variables) Declaration is called a “prototype” Specifies the name, parameters and return type of the function, without the function body.

Prototype Example #include <stdio.h> int imax(int,int); int main(void) { printf("The maximum of %d and %d is %d.\n", 3, 5, imax(3,5)); return 0; } int imax(int n, int m) { int max; if (n > m) max = n; else max = m; return max;

Prototyping of Functions Why use prototypes? They enable the compiler to catch many errors, or oversights, you might make using a function

Example: Continuing Fraction Given n and terms, compute: y = 1 + n . . .

Example: Continuing Fraction Example: If n is 2, and terms is 1: y = 1 + n = 1 + 2  1.33333

Example: Continuing Fraction Given n and terms, compute: Fraction(n, terms) y = 1 + n . . .

Example: Continuing Fraction 1 Sum(n, terms) 1 1 + 1 1 + . . . n 1 +

Example: Continuing Fraction 1 1 + Fraction(n, terms - 1) 1 1 + . . . n 1 +

Example: Continuing Fraction Last term: 1 n Sum(n, 1) 1 +