Download presentation
Presentation is loading. Please wait.
Published byἌλκανδρος Παπανδρέου Modified over 5 years ago
1
Introduction to Computing Lecture 09: Functions (Part II)
Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
2
Topics Structured Programming Scope Prototypes C Library Functions
3
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
4
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).
5
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
6
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
7
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.
8
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
9
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
10
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
11
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;
12
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;
13
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.
14
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.
15
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.
16
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
17
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.
18
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;
19
Prototyping of Functions
Why use prototypes? They enable the compiler to catch many errors, or oversights, you might make using a function
20
Example: Continuing Fraction
Given n and terms, compute: y = 1 + n . . .
21
Example: Continuing Fraction
Example: If n is 2, and terms is 1: y = 1 + n = 1 + 2
22
Example: Continuing Fraction
Given n and terms, compute: Fraction(n, terms) y = 1 + n . . .
23
Example: Continuing Fraction
1 Sum(n, terms) 1 1 + 1 1 + . . . n 1 +
24
Example: Continuing Fraction
1 1 + Fraction(n, terms - 1) 1 1 + . . . n 1 +
25
Example: Continuing Fraction
Last term: 1 n Sum(n, 1) 1 +
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.