Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
201801 CSE240 Lecture 11
1. CSE240 – Introduction to
Programming Languages
Lecture 11:
Programming with C | Functions calls and parameter passing
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
Scope of Declaration
•Scope Rule: The scope of a variable starts from its declaration point
and extends to the end of the current block in a pair of braces.
•Declaration-before-use: Variables and functions must be declared
before they are used.
{
int height = 6; int width = 6;
int area = height * width;
// . . .
} // block ends
14. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14
Scope Rule: Forward Declaration
void bar(void); // forward declaration to satisfy scope rule
int foo(void); // forward declare all functions
int foo() { // genuine declaration
. . .
bar(); // call function bar()
. . .
}
void bar() { // genuine declaration
. . .
k = foo() // call function foo()
. . .
}
15. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15
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.
16. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16
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.
20. CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.