{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week04--newLecture1 - Subprograms Memory Management...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Subprograms & Memory Management Principles of Programming Computer Engineering 2SH4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Scope of Names Scope of a Name – the region in a program where a par4cular meaning of a name is visible. The scope of a local variable begins at its declara4on and con4nues to the closing brace of the func4on in which it is deFned. The scope of a global variable begins at its declara4on and con4nues to the end of the source Fle. ( poor programming style – not structured ) The scope of a func-on name begins with its prototype and con4nues to the end of the source Fle. 2
Background image of page 2
3 /*Outline of Program for Studying Scope of Names*/ #define MAX 950 #define LIMIT 200 void one(int x, double y); /* prototype 1 */ int fun_two(int z, char x); /* prototype 2 */ double varglb1; /* global variable */ int main(void){ /* main function */ int localvar; /* local to main */ . . . } /* end main */ void one(int anarg, double second) { /* function header 1 */ int onelocal; /* local to f1 */ . . . } /* end one */ int varglb2; /* global variable */ int fun_two(int one, char anarg) { /* function header 2 */ int localvar; /* local to f2 */ . . . } /* end fun_two */
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 /* Simple example of variable name scope */ #include <stdio.h> void testscope(int j); int i = 0; /* global declaration */ int main(void) { int j = 0; /* local declaration */ i++; testscope(i); printf("Testing the scope of variables i and j\n"); printf("i = %d, j = %d\n", i, j); return(0); } void testscope(int j) { /* formal paramter = local */ for(i=0; i<100; i++) { j + + ; } }
Background image of page 4
Returned Values as Actual Arguments Func4ons that return values may be used as actual arguments to other func4ons. for example: printf(“The factorial of 6 is %d.\n”, factorial(6)); /* assuming we have written function factorial */ /* which we’re about to… */ 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Design Problem 6 Write a program to calculate the factorial of numbers 1 to 10 and print the result to the screen. Write a user deFned func4on for the factorial calcula4on using the provided prototype. Trace by hand the execu4on of the Frst 3
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 23

Week04--newLecture1 - Subprograms Memory Management...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online