{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week04--newLecture1 - Subprograms Memory Management...

Info icon This 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
Image of page 1

Info icon This 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 defined. The scope of a global variable begins at its declara4on and con4nues to the end of the source file. ( poor programming style – not structured ) The scope of a func-on name begins with its prototype and con4nues to the end of the source file. 2
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 */
Image of page 3

Info icon This 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++; } }
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
Image of page 5

Info icon This 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 defined func4on for the factorial calcula4on using the provided prototype. Trace by hand the execu4on of the first 3
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern