PHP – function use

As with the previous post about the use method within php (here), you are also able to use the use method attached to a callable function as in

function ($parameter) USE ($somevalue)

So you are able to use any variables that are within scope within the new function, below is an example where I am using the array walk methods callback (which means call a function that is available). The function is using the values within the array ($arrays 4 and 5) and adding them to the $value variable that is passed within the use parameters (here I am using the & symbol which means to pass in reference and not just a copy of the value (you are able to alter the actual parameter passed), so in this instance the $value starts of with 1, then adds 4 and then 5 to it which in turn the result is 10.

$value = 1;
$callback = function ($one) use (&$value) {
	$value += $one;
};
$arrays = array(4,5);
array_walk($arrays, $callback);
print_r("value : " .$value);

And the output would be.

value : 10

Function vs Method

The main difference between a function and a method is that a function can live independently of the any instance of a class, where as a method sits within a class.

That is about it, e.g. a function is

int func(int value);
...
 
int main()
{
  cout << funct(3) << endl;
}

whereas a method has to live within a class

class myclass
{
    int myMethod(int it);
};
 
....
 
int main()
{
   myclass theclass;
   cout << theclass.myMethod(4) << endl;
}

Static in c – hide that function

In the c language there is no such thing as a class, private/public setup as such, but if you have a allot of c files that may have allot of the same function names inside e.g. swap(void *data, void *data2). So how do you just want to call your swap function and not another one from another c file. Which in this setup it is similar to private method within a class.

So instead of having access to the private keyword, you can define a function to be a static function as below in this header file that I have called static_test.h

#ifndef STATIC_TEST_H
#define STATIC_TEST_H
 
#include <iostream>
using namespace std;
 
static void PrivateAccessable(char *sting);
 
void PublicAccessable(char *st);
 
#endif // STATIC_TEST_H

the PublicAccessable function is what is *exposed* to other parts of the main program but the static void PrivateAccesable is not.

So if you try and access it via the int main function like so

 
#include <iostream>
#include "static_test.h"
 
int main(int argc, char *argv[])
{
 
    PublicAccessable("hi there");
    // undefined reference to `PrivateAccessable(char*)'
    PrivateAccessable("should not be able to access this!!");
 
    return 0;
}

The compiler will complain with something similar to the error above the PrivateAccessable line, or

undefined reference to `PrivateAccessable(char*)'

because the linker does not know where that function is, since it is “hidden” within the static_test namespace as such.

Just to complete the code example here is the static_test.c

#include "static_test.h"
 
static void PrivateAccessable(char *sting)
{
    cout << sting << endl;
}
 
void PublicAccessable(char *st)
{
    PrivateAccessable(st);
}

Function arguments – c++

There are sometimes that you want to pass more than a defined number of parameters to a function (arguments). Of course you can pass a array of parameters or just use a inbuilt stdarg.h library. This allows you to pass allot of arguments into a function, but without defining them at compile time.

For example if you want to add up a list of numbers and run time, but do not know how many to pass at the compile time then you define the function method as

int sumvalues(int numbersToBePassed, ...);

The important part is the “…”, you have to always pass in the first parameter at least, because at run time this parameter will tell the function how many additional parts to the function there will be.

Then to setup the argument list, you use the c++ library stdarg.h and this will include the function/methods that will allow this to happen.

To start with you will need to setup the argument list,

va_list args

This will setup the variable args to contain the list of additional arguments passed, then to setup where to start to pull arguments from, e.g. where the last parameter named at compile time will know, if the parameter list is like

int sumedvalues(int value1, int value2, int additionalValues, ...)

then the additionalValues is the last parameter that the compile time will know about, so to setup the pointer to the next parameter for the args variable

// for the above example
va_start(args, additionValues);
//for the code example
va_start(args, numbersToBePassed);

to pull out a argument you just need to use the va_arg function, its parameters are

va_arg(va_list, type);

where the va_list is the args variable and the type is the type that you want back, e.g. int or float.

To complete the argument list you need to end the argument pull by

va_end(va_list)

where again the va_list would be args in this example.

Here is some code that will demo the above as well.

#include <iostream>
#include <stdarg.h>
 
using namespace std;
 
int sumvalues(int numbersToBePassed, ...)
{
  int sumd =0;
  va_list args;
  va_start(args,numbersToBePassed);
 
  for (int i =0; i < numbersToBePassed; i++)
  {
    sumd +=va_arg(args,int);
  }
  va_end(args);
  return sumd;
}
 
int main()
{
  cout << "Add the values 5 4 3 5 together = " << sumvalues(4, 5,4,3,5) << endl;
  return 0;
}

With the output being

Add the values 5 4 3 5 together = 17