+ All Categories
Home > Documents > CS 332 Programming Language Concepts

CS 332 Programming Language Concepts

Date post: 21-Jan-2022
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
13
March 18, 2020 © Sam Siewert CS 332 Programming Language Concepts Lecture 9 Subroutines (Continued)
Transcript
Page 1: CS 332 Programming Language Concepts

March 18, 2020 © Sam Siewert

CS 332

Programming Language Concepts

Lecture 9 – Subroutines

(Continued)

Page 2: CS 332 Programming Language Concepts

Exam #1

Ave=74% [Last year was 76%]

You can Re-do Programming (for 80-90% credit)– 80% if your original answer was not fully correct

– 100% if your original answer was correct and I could not understand it –you prove it can be implemented and tested

– 60% if you ran out of time and left the problem blank, but can fully implement and test your own unique solution

If your trouble was not on Code, Do Flag Problem!

Exam #1 is worth 15% of your grade

Assignment practice, participation and scaffold is 40%

Final Exam worth 20% [presentation]

Overall policies – Active Learning Grading Policy

© Sam Siewert 2

Page 3: CS 332 Programming Language Concepts

Exam #1 – Take Away#1 - Compilers generate ASM code according to an ABI (function call, basic block, return)

#2 – Regular expressions (3 rules – Kleene*, Concatenation, Alternation) and Well written CFG (adds recursive rules to Regex), ideally non-ambiguous

#3 – Imperative Procedural C features - void*, function pointer, and union provide ability to avoid code duplication, but not OO compiler or run-time polymorphism of C++ or Java

#4 – Syntactic ambiguity does not guarantee semantic ambiguity in final evaluation (can cause ambiguous evaluation order and final evaluation)

#5 – Semantic analysis – translation of unambiguous post-fix to unambiguous in-fix expressions (evaluate post-fix, generate in-fix) – Use scanner/parser, stack, simplification (evaluation)

#6 – Data types, control flow, procedural abstraction – array is homogeneous aggregate of base types with memory structure (row or column major) –transform by procedure

#7 – Data type extension – C union memory structure and use (non-homogeneous aggregate with variant representation)

© Sam Siewert 3

Page 4: CS 332 Programming Language Concepts

Exploration Questions from Before…

Why Does ANSI C not support Nested Functions?

Why does GCC support anyway?

Why does GCC also not support in C++ too?

Does Java Support Nested Functions?

C#?

Python?

Ada?

Others?

Why are Nested Functions Useful?

© Sam Siewert 4

Page 5: CS 332 Programming Language Concepts

Parameter PassingMapping of Actual to Formal Arguments1. Applicative Order – Arguments are evaluated before passing to a

subroutine

2. Normal Order – Representation passed as an argument that is NOT evaluated until (and if) the value it actually used

By Value – C Default

By Value/Result (Copying), Scripting Languages

References (Aliases) – Using Pointers or Access/Ref Operators in Strongly Typed PLs

Closure, name – Data structure storing a function with its context (environment for bindings) – PLP P. 153 & 401, Call by Name – PLP P. 275 Normal Order Evaluation & P. 402

Interesting Side Excursions – Clojure, DrJava, Racket, …, More Free Compilers and Interpreters

© Sam Siewert 5

Page 6: CS 332 Programming Language Concepts

Pass by Value vs. Reference

Two Most Common Methods (can be confusing)

– By Value – Actual is assigned to Formal and After, the Two are

Independent

– By Reference – A new name for corresponding Actual (alias

assuming actual name is visible) and change to either name

changes both!

Common Use in Popular PLs

– C is Pass by Value (and Pointers/Address are used for

Reference)

– C++ introduces a reference in addition to pointers, but default is

still pass by value

– Java and Ada prefer to abstract pointers

– Java uses object references, however is still pass by value

– Ada allows specification of either using IN, OUT, IN OUT

© Sam Siewert 6

Page 7: CS 332 Programming Language Concepts

C and C++ Swap with

Pointers vs. References

© Sam Siewert 7

#include <stdio.h>

void swapnum(int &i, int &j)

{

int temp = i;

i = j;

j = temp;

}

int main(void)

{

int a = 10;

int b = 20;

printf("A=%d and B=%d\n", a, b);

swapnum(a, b);

printf("A=%d and B=%d\n", a, b);

return 0;

}

C++ C#include <stdio.h>

void swapnum(int *i, int *j)

{

int temp = *i;

*i = *j;

*j = temp;

}

int main(void)

{

int a = 10;

int b = 20;

printf("A=%d and B=%d\n", a, b);

swapnum(&a, &b);

printf("A=%d and B=%d\n", a, b);

return 0;

}

Parameter refs,

Simple swap

Parameter ptrs,

Swap with derefs

(confusing?)

Simple variable actuals Address of variable actuals

Page 8: CS 332 Programming Language Concepts

Bad SwapMust use Reference or Only Copies are Swapped

C++ Java??

© Sam Siewert 8

#include <stdio.h>

void badswap(int i, int j)

{

int temp = i;

i = j;

j = temp;

}

int main(void) {

int a = 10;

int b = 20;

printf("A=%d, B=%d\n", a, b);

badswap(a, b);

printf("A=%d, B=%d\n", a, b);

return 0;

}

public class swapper

{

public static void main(String[] args)

{

int a=1, b=2;

System.out.format("a=%d b=%d\n", a, b);

badSwap(a,b);

System.out.format("a=%d b=%d\n", a, b);

}

private static void badSwap(int a, int b)

{

int temp = a;

a = b;

b = temp;

}

}Compiles,

Runs,

??

Compiles,

Runs,

Swaps what??

Page 9: CS 332 Programming Language Concepts

Simple Java SwapSwap inside Object (privately!)

© Sam Siewert 9

public class swapper

{

private static int a=1, b=2;

public static void main(String[] args)

{

System.out.format("a=%d and b=%d\n", a, b); badSwap(a,b);

System.out.format("a=%d and b=%d\n", a, b); goodSwap();

System.out.format("a=%d and b=%d\n", a, b);

}

private static void badSwap(int var1, int var2)

{

int temp = var1;

var1 = var2;

var2 = temp;

}

private static void goodSwap()

{

int temp = a;

a = b;

b = temp;

}

}

Page 10: CS 332 Programming Language Concepts

Subroutine ClosuresCreate a Reference for a Subroutine and Pass as a

Parameter

– Scope is where Function is Elaborated?

– Or where it is Invoked?

Subroutines are Routinely Passed as Parameters in

Functional Languages

Let’s Look at C qsort and function pointers to start

– Before OO, this Method Used to Sort any Type

– Use of void * and function pointer for comparison

– Frowned upon today, but why?

© Sam Siewert 10

void qsort(void *base, size_t nitems, size_t size,

int (*compar)(const void *, const void*))

Page 11: CS 332 Programming Language Concepts

C Function Pointers - ConfusingRegister a function as a callback, no parameters - e.g. ISR in Micro

More complicated if function includes parameters

© Sam Siewert 11

#include <stdio.h>

void funcall(void (*anyfunct)()) {

anyfunct();}

void test() {

printf("hello world\n");}

int main() {

funcall(test);return 0;

}

#include <stdio.h>

void funcall(void (*anyfunct)(int, double), int x, double y){

anyfunct(x, y);}

void test(int a, double b){

printf("hello world, a=%d, b=%lf\n", a, b);}

int main(){

int someint=77; double somedouble=77.0;

funcall(test, someint, somedouble);

return 0;}

Page 12: CS 332 Programming Language Concepts

C qsort – old school ADT

© Sam Siewert 12

#include <stdio.h>

#include <stdlib.h>

int values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)

{

return ( *(int*)a - *(int*)b );

}

void main(void)

{

int n;

printf("Before sorting the list is: \n");

for( n = 0 ; n < 5; n++ ) {

printf("%d ", values[n]);

}

qsort(values, 5, sizeof(int), cmpfunc);

printf("\nAfter sorting the list is: \n");

for( n = 0 ; n < 5; n++ ) printf("%d ", values[n]);

}

Associate method

For type of interest

(here two integers)

Array of any type

Number of elements

Size

Generic function pointer

Anonymous types

Page 13: CS 332 Programming Language Concepts

Alternative to Function Pointers are

Ada Generic or C++ Template

Common Algorithm

Parametric Instantiation with a Given Type to Operate

On

Generally Cleaner and Less Confusing than void * and

function pointers

Avoids replication of Code for each Type Algorithms is

Applied to (ADT)

© Sam Siewert 13


Recommended