Salta al contenuto
Note per Studenti Functions, Variables and Scope Report

Functions, Variables and Scope

In questa pagina 4

The deck is titled "functions declaration and definition" and also announces scope rules, but its slides contain no dedicated section on scope or storage classes: only the notes on declarations below. Do not rely on these notes for scope rules.

Variables

Functions

A function encapsulates a computation that can be used without caring about its implementation: it is enough to know what it does, not how. It can be called repeatedly. Functions are also how problems are decomposed into sub-problems (Introduction to C and the Development WorkflowWhat a computer and a program are, how algorithms are specified with preconditions and postconditions, why C is a compiled language, and the structure of a first C program.Introduction to C and the Development Workflow →).

Definition

return-type function-name(parameter declarations, if any)
{
    declarations
    statements
}

Example from the slides: C has no exponentiation operator, so write power(m, n) raising an integer to a non-negative power.

c
#include <stdio.h>
int power(int base, int n);          /* prototype */

main()                               /* test power function */
{
    int i;
    for (i = 0; i < 10; ++i)
        printf("%d %d %d\n", i, power(2,i), power(-3,i));
    return 0;
}

/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
{
    int result = 1;
    for (int i = 1; i <= n; ++i)
        result *= base;
    return result;
}
  • result starts at 1 (any number to the power 0 is 1); the loop multiplies by base n times.
  • Each call passes two arguments and gets an int back to be formatted by printf.
  • Negative exponents: return a floating value.
c
double power(double base, int exponent) {
    double result = 1.0;
    int positive_exp = (exponent < 0) ? -exponent : exponent;  // absolute value
    for (int i = 0; i < positive_exp; i++) {
        result *= base;
    }
    return (exponent < 0) ? 1.0 / result : result;
}

Parameters, arguments, prototype

  • Parameters are variables named in the function's declaration; arguments are the values passed at call time, assigned to the parameters.
  • A prototype such as int power(int base, int n); placed before main says power expects two ints and returns an int. Parameter names are optional (int power(int, int);) but good names document the function.
  • Function definitions may appear in any order and in one or several source files, but a function cannot be split between files.

return

Passing arguments: call by value

Function pointers

A function pointer stores the address of a function and calls it indirectly.

c
#include <stdio.h>
void greet() {
    printf("Hello, world!\n");
}
int main() {
    void (*func_ptr)();   // pointer to a function with no arguments returning void
    func_ptr = greet;     // address of the function
    func_ptr();           // indirect call
    return 0;
}

Syntax: return_type (*pointer_name)(parameter_types);, e.g. int (*operation)(int, int);. As a parameter:

c
int add(int a, int b) { return a + b; }
int operate(int x, int y, int (*func)(int, int)) { return func(x, y); }
int main() {
    int result = operate(5, 3, add);
    printf("Result: %d\n", result);  // Result: 8
    return 0;
}

Versione ripasso

Teoria collegata