Functions and Parameter Passing § A function is a named block of code that must be explicitly called. § The purposes of using functions are twofold: abstraction and reuse: abstraction: statements that form a conceptual unit; reuse: statements that can be executed in more than one place in the program. § Functions communicate with the rest of the program by using either global variables or parameters / return value § Formal and actual parameters. In the declaration formal parameters. When calling a function, actual parameters are given.
Parameter Passing § Call-by-value: a formal parameter is a local variable in the function. It is initialized to the value of the actual parameter. It is a copy of the actual parameter. Advantage: no side-effects (safe, reliable). Drawback: less flexible/less powerful. § Call-by-address (pointer or reference): the formal parameter is a pointer to the actual parameter. There is only one variable with two names. Changing the formal parameter immediately changes the actual parameter. Reference in C++ only. Drawback : side-effects (programmer skills) Advantage: flexible/powerful.
Structure § Provide 3 examples of lexical errors in C. § Provide 3 examples of syntactic errors in C. § Provide 3 examples of semantic errors in C. § Provide 3 examples of lexical errors in C++. § Provide 3 examples of syntactic errors in C++. § Provide 3 examples of semantic errors in C++. § Provide 3 examples of lexical errors in Java § Provide 3 examples of syntactic errors in Java § Provide 3 examples of semantic errors in Java § Exemplify a semantic error in Java that is NOT a semantic error in C/C++ § Given char a [5] = {'a', 'b', 'c', '\0'}; Is there is an error in the following line? char * b = a; If so, is it lexical, syntactical, or semantical?
Paradigms § Provide 3 characteristics/features of an imperative programming language § Provide 3 characteristics/features of a functional programming language § Provide 3 characteristics/features of a logic programming language § Provide 3 characteristics/features of an object-oriented programming language § Mention 3 differences (regarding features or capabilities) between C++ and Java (both are object-oriented programming languages).
Recursion § Create a function that print the following. It should be recursive, i.e., DO NOT use for, do/while or while statements here. 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1,
Arrays § Did you now the tic-tac-toe game? Create a 3x3 array to store a tic-tac-toe. Fill the array with ‘X’ or ‘O’. All the cells with the same value. Which data type should be used? Provide your source code using C § Use the language C. Create a struct named ball. Put 2 variables inside: an array of chars to store a color and an integer to store an id. § Ask the user for a number using scanf. § Use the number that the user provides to create an array of balls. The number represent how many elements the array should have. § Store in the first position of the array a ball with color= blue and id=1.
Arrays § Write a C program that, given an array a[ ] with size n and another number x, determines whether there exist two elements in a [ ] whose sum is exactly x. It returns 0 (the two elements do not exist) or 1 (the two elements exist). § Write a C program that, given an array of ball (see question 5) named a[] of n items, prints the id of all the items with color==red.
Pointers § In the following code: #include <stdio.h> int main() { int var =10; int *p; p= &var; printf (“\n %p", &var); printf (“\n %p", p); printf (“\n %p", &p); printf (“\n %p", p); printf (“\n %d", var); printf ("\n %d", *p); printf (“\n %d", *( &var)); } Could you explain what is being printed for each printf instruction?
[email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.