Engineering computing i
1 / 27

Engineering Computing I - PowerPoint PPT Presentation

  • Uploaded on

Engineering Computing I. Chapter 4 Functions and Program Structure. Functions. Functions break large computing tasks into smaller ones Functions enable programmers to build on what others have done instead of starting over from scratch.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Engineering Computing I' - heath

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Engineering computing i

Engineering Computing I

Chapter 4

Functions and Program Structure


  • Functions break large computing tasks into smaller ones

  • Functions enable programmers to build on what others have

  • done instead of starting over from scratch.

  • Appropriate functions hide details of operation from parts of

  • the program that don’t need to know about them thus

  • clarifying the whole, and easing the pain of making changes.

  • C has been designed to make functions efficient and easy to

  • use

  • C programs generally consist of many small functions rather

  • than a few big ones.

Chapter 4

Basics of functions
Basics of Functions

To begin with, let us design and write a program to print each line of its input that contains a particular ‘‘pattern’’ or string of characters. (This is a special case of the UNIX program grep.) For example, searching for the pattern of letters ‘‘ould’’ in the set of lines

Ah Love! could you and I with Fate conspire

To grasp this sorry Scheme of Things entire,

Would not we shatter it to bits -- and then

Re-mould it nearer to the Heart’s Desire!

will produce the output

Ah Love! could you and I with Fate conspire

Would not we shatter it to bits -- and then

Re-mould it nearer to the Heart’s Desire!

Chapter 4

Function definition
Function Definition

Minimal Function

Chapter 4

External variables
External Variables

  • A C program consists of a set of external objects, which are either variables or functions.

  • The adjective ‘‘external’’ is used in contrast to ‘‘internal’’, which describes the arguments and variables defined inside functions.

  • External variables are defined outside of any function, and are thus potentially available to many functions.

  • Functions themselves are always external, because C does not allow functions to be defined inside other functions.

  • Because external variables are globally accessible, they provide an alternative to function arguments and return values for communicating data between functions.

Chapter 4

Scope rules
Scope Rules

The scope of a name is the part of the program within which the name can be used

Func1(int x, int y) {

int a;


for(int j=0; j<10; j++)

a = 1;


j = 0;


Local or automatic variables:

X, y , a and j

Scope of local variabe j

Scope of local variabes x, y, and a

Chapter 4

Multiple source files
Multiple Source Files

extern int d, e f;

int a;

void func1(args1);

void func2(args2) ;

extern void func3(args1);

extern void func4(args2) ;

main(…) { …}

int b;

extern int c;

func1(args1) {…}

int c;

func2(args1) {…}

int d;

extern void func1(args1);

extern void func2(args2) ;

int e;

extern int f;

func3(args1) {…}

int f;

func4(args1) {…}

Chapter 4

Header files
Header Files

Chapter 4

Header files1
Header Files



#include <stdio.h>

#include “calc.h”

#define MAXVAL 100

Int sp = 0;

Double val[MAXVAL]

void push(double)


#include <stdio.h>

#include <stdlib.h>

#include “calc.h”

#define MAXDP 100

main() {


Chapter 4

Register variable
Register Variable

  • A register declaration

  • advises the compiler that the variable in question will be heavily used.

  • The idea is that register variables are to be placed in machine registers, which may result in smaller and faster programs

Chapter 4

Register variable1
Register Variable

A = A+B



Register B

Register A

Register D

Register C

Register F

Register E

Chapter 4

Block structure
Block Structure

Chapter 4


Chapter 4


Size is omitted

Chapter 4



Chapter 4

The c preprocessor
The C Preprocessor

C provides certain language facilities by means of a preprocessor, which is conceptionally a separate first step in compilation:

  • #include

  • #define

  • Conditional Compilation

  • Macros with Arguments

Chapter 4

File inclusion
File Inclusion

  • They include common #define statements and extern declarations and function prototypes

  • There are often several #include lines at the beginning of a source file

  • #include is the preferred way to tie the declarations together for a large program

  • It guarantees that all the source files will be supplied with the same definitions and variable declarations

  • when an included file is changed, all files that depend on it must be recompiled

Chapter 4

Macro substitution
Macro Substitution

#define name replacement text

name = replacement text

Chapter 4

Conditional inclusion
Conditional Inclusion

  • It is possible to control preprocessing itself with conditional statements.

  • This provides a way to include code selectively, depending on the value of conditions evaluated during compilation.

  • The #if line evaluates a constant integer expression.

  • If the expression is non-zero, subsequent lines until an #endif or #elif or #else are included.

Chapter 4

Conditional inclusion2
Conditional Inclusion

The #ifdefand #ifndeflines are specialized forms that test whether a name is defined. The first example of #if above could

have been written

Chapter 4