Procedures and functions

From Wikiversity

Jump to: navigation, search

[edit] Overview

Programming can be a repetitive task. While we have structures like loops to help us do the exact same thing many times, there are times when we need to do something different with only slight variation each time. Rather than write the same code with only minor differences over and over, we group the code together and use a mechanism to allow slight variations each time we use it.

A function (also called procedure or method) is a smaller program with a specific job. In most languages they can be "passed" data, called parameters, which allow us to change the values they deal with. Languages usually have a way to return information, and this is called the return data.

The programmer uses a function by calling it. For example

//eg C program

void print_my_num(int in)//<--- the function is declared, showing the placeholder where the data is passed (parameters)
{
    printf("%i",in);
}

main()
{
    print_my_num (100);//<---calling the function
}

/*WHAT WOULD SHOW UP ON THE SCREEN*/
100

An example of "returning" could look like this:

int double_my_num (int in)
{
   return 2*in;
}

main()
{
   printf("%i",double_my_num(5));
}

/*WHAT WOULD SHOW UP ON THE SCREEN*/
10

Parameters are often referred to as arguments. A fine distinction between parameters and arguments can be made, however. A parameter (or formal parameter) is a characteristic of a function while an argument (or actual parameter) is a characteristic of a function call. A parameter exists for a function, even without enclosing source code while the argument exists only in a running program when a call to the function is made.

Parameters can be passed into a function with different semantics. The different ways to pass parameters are called call-by-value, call-by-strict-value, call-by-reference, call-by-name, call-by-result and call-by-value-and-result. The first three are the more common forms.

Call-by-value 
Call-by-value describes passing a parameter to a function as its value. A variable that is passed call-by-value is duplicated and exists both in the calling code and as an argument value inside the function. A call-by-value argument doesn't have to be a variable; it can also be the result of an expression, in which case the only accessible form is the argument value. The call-by-value argument ceases to exist with the end of the function call, unless assigned to a parameter that survives the end of the function call or returned as the return value of the function call. Examples: C, VB's ByVal.
Call-by-strict-value 
Call-by-strict-value is a variant of call-by-value where the function cannot assign a new value to the parameter.
Call-by-reference 
Call-by-reference means that a reference to the argument value is passed into a function. The function is told the location of the value instead of the value itself. The most noteworthy difference to call-by-value is that the calling code will continue to work with the argument value as it was last modified by the function call. While a variable passed call-by-value is duplicated, a variable passed call-by-reference is not duplicated and the algorithm works with the original variable. A call-by-reference argument has to be a variable and cannot be the result of an expression. Examples: C with pointer, VB's ByRef.
Call-by-name 
Call-by-name refers to passing a parameter as the name of a variable. The function resolves the name to a value while executing.
Call-by-result 
Call-by-result is rather a different way to pass a result value than a way to pass argument values. The argument must be a variable that can, at the end of the function call, receive the value that was assigned to the parameter by the fnction.
Call-by-value-and-result 
Call-by-value-and-result combines call-by-value and call-by-result.
Call-by-Sharing 
Call-by-Sharing means that the argument within the caller and the callee is a shared object. Example: Python.