I really have no idea how this is done, it should be simple but i just cant get it
i have an IBAction and void and want to do this:
-(IBAction)pass{
int VariableX = 10;
[self getVar]; ---> send var to -(void)getVar
}
-(void)getVar{
get the VariableX
if(VariableX=10){
do something
}
}
declare getVar function to get one (integer) parameter:
// header
-(void)getVar:(int)varX;
// implementation
-(void)getVar:(int)varX{
if (varX == 10)
// do something
}
Then call it the following way:
-(IBAction)pass{
int VariableX = 10;
[self getVar:VariableX];
}
Generally the syntax of method declaration in objective-c is:
- (ReturnType) functionName:(1st parameter type)1stParameterName
2ndParameter:(2nd parameter type)2ndParameterName
etc
Related
I'd like to generate logging messages from within a C function embedded in a DML method. Take the example code below where the fib() function is called from the write() method of the regs bank. The log methods available to C all require a pointer to the current device.
Is there a way to get the device that calls the embedded function? Do I need to pass the device pointer into fib()?
dml 1.2;
device simple_embedded;
parameter documentation = "Embedding C code example for"
+ " Model Builder User's Guide";
parameter desc = "example of C code";
extern int fib(int x);
bank regs {
register r0 size 4 #0x0000 {
parameter allocate = false;
parameter configuration = "none";
method write(val) {
log "info": "Fibonacci(%d) = %d.", val, fib(val);
}
method read() -> (value) {
// Must be implemented to compile
}
}
}
header %{
int fib(int x);
%}
footer %{
int fib(int x) {
SIM_LOG_INFO(1, mydev, 0, "Generating Fibonacci for %d", x);
if (x < 2) return 1;
else return fib(x-1) + fib(x-2);
}
%}
I want to log from an embedded C function.
I solved this by passing the Simics conf_object_t pointer along to C. Just like implied in the question.
So you would use:
int fib(conf_object_t *mydev, int x) {
SIM_LOG_INFO(1, mydev, 0, "Generating Fibonacci for %d", x);
}
And
method write(val) {
log "info": "Fibonacci(%d) = %d.", val, fib(dev.obj,val);
}
Jakob's answer is the right one if your purpose is to offload some computations to C code (which makes sense in many situations, like when functionality is implemented by a lib).
However, if you just want a way to pass a callback to an API that asks for a function pointer, then it is easier to keep the implementation within DML and use a method reference, like:
method init() {
SIM_add_notifier(obj, trigger_fib_notifier_type, obj, &trigger_fib,
&dev.regs.r0.val);
}
method trigger_fib(conf_object_t *_, lang_void *aux) {
value = *cast(aux, uint64 *);
local int result = fib(value);
log info: "result: %d", result;
}
method fib(int x) -> (int) {
log info: "Generating Fibonacci for %d", x;
if (x < 2) return 1;
else return fib(x-1) + fib(x-2);
}
I’m a newbie in programming language and I’m learning Javascript. I’m trying to understand the concept of callback function. I realized that callback is intended a function passed as parameter, but when does it call?
In the below examples I used a classic approach to write functions and then I tried to use the arrow function. The callback is done() function, in the first example it is called after the parent function, in the second one after.
What is the reason? Can you give me an explanation? Thank you so much for the feedback
Example no. 1
function done(){
console.log("Done");
}
function increment(num, callBack){
for(var i = 0; i <= num; i++){
console.log(i);
}
return callBack();
}
increment(10, done);
Example no. 2
const done = () => {
console.log("Done");
}
const increment = (num, done) => {
for (var i = 0; i <= num; i++) {
console.log(i);
}
}
increment(10, done());
To use callback you need to specify that there'll be a callback function as a argument, and you need to call that somewhere in the parent function like this, and that's when it will be called:
function someFunction(callback){
//Do something if needed...
callback() //callback(someParameter) if you want to pass some parameter to the callback func
//Do something if needed...
}
In your 2nd example, it's actually not a proper way of using callback function because you did not call the callback function inside parent function. You can modify it to to make it work as 1st example like this:
const increment = (num, done) => {
for (var i = 0; i <= num; i++) {
console.log(i);
}
done(); //call the callback function
}
increment(10, done); //just pass the name of callback func, not call it like you did "done()"
for ( i = 0; i < 100; i++) {
browser.manage().logs().get('browser').then(function(browserLog) {
console.log(i);
});
}
I am trying to run this using protractor, but i get hundred times 100 printed in console. I have some functionality which am trying to implement using looping.How can i do looping in protractor?
This is because the functions you're passing to then all close over the variable i, not the value that variable has when you create the functions. So later, when the functions are called, they all see the value of i as it is then, after the loop is complete (100).
If you want to capture the value of i as it is when you create the function, you can use ES5's Function#bind:
for ( i = 0; i < 100; i++) {
browser.manage().logs().get('browser').then(function(index, browserLog) {
console.log(index);
}.bind(null, i));
}
bind returns a new function that, when called, will call the original function with a given this value (in this case I'm using null) and any arguments you follow that with, followed by arguments given to the function bind returned.
Another approach is a builder function:
for ( i = 0; i < 100; i++) {
browser.manage().logs().get('browser').then(buildHandler(i));
}
function buildHandler(index) {
return function(browserLog) {
console.log(index);
};
}
That has the advantage of allowing the caller to control this.
I'm trying to create a function to calculate the mean, max, min and standard deviation of a set of numbers.
I'd like it to work like the UIColor function - (void)getRed:green:blue:alpha. i.e. you pass in the four float values and the function then overwrites them.
I'm struggling to find the right syntax for it.
My function is...
- (void)calculateStatsAverage:(float)average
standardDeviation:(float)standardDeviation
minimum:(float)minimum
maximum:(float)maximum
{
//pseudo code
average = total / count;
minimum = min value;
etc...
//
}
The problem I'm getting is getting the values out again.
If I change the function to use float* (which is what the UIColor function does) then my calculations don't like assigning the variables.
To simplify...
Imagine these functions. The first is called from elsewhere.
- (void)runThisFunction
{
float someOutputValue = 0.0;
[self changeTheValue:someOutputValue];
NSLog(#"The value is %f", someOutputValue);
}
- (void)changeTheValue:(float)value
{
value = 10.0;
}
I'd like this code to output "The value is 10.0"; But at the moment I'm getting "The value is 0.0".
Please could you show me how to write these two functions. From there I'll be able to work out the rest.
Thanks
- (void)passByRefMethod:(float *)ptr
{
*ptr = MYVALUE;
}
Sorry for formatting, typed on phone. Hope this helps!
This technique is often called pass by reference, and it's part of C so you can use that to search for more info.
Your NSLog is displaying the value of the local variable 'someOutputValue', which has been assigned to be 0.0.
Your 'changeTheValue' method has no effect.
The following code may help,
- (void)runThisFunction
{
float someOutputValue = 0.0;
float resultOfCalc = [self calcTheValue:someOutputValue];
NSLog(#"The value is %f", resultOfCalc);
}
- (float) changeTheValue:(float)value // note that this method returns a float
{
float newValue;
// do whatever calc is appropriate, e.g.
newValue = value + 10.0;
return newValue; // pass back the result of calc
}
This will output,
The value is 10.
I'm really newbie developing iphone apps, and I got a question...
I would like to know how must I do to receive the var that my function returns.
Take a look:
-(void)myfunc{
float num1 = 2.50;
float num2 = 3.50;
//here is my doubt, how can I get the value that function returns?
float varreceived = [self getNumber:num1:num2];
}
//the function that receive the vars and returns a value
-(float)getNumber: (float)var1 :(float)var2 {
NSLog(#"1->%f",var1);
NSLog(#"2->%f",var2);
return 3.23;
}
I receive this error when I build:
"error: void value not ignored as it ought to be"
in this line:
float varreceived = [self getNumber:num1:num2];
Update:
Nop, I receive this error when I build:
"error: void value not ignored as it ought to be"
in this line:
float varreceived = [self getNumber:num1:num2];
Max:
then you probably declared the -(float)getNumber: (float)var1 :(float)var2 as -(void)getNumber: (float)var1 :(float)var2 in your header
HispaJavi:
Yeah!! this is the problem, I wrote "-(void)getNumber" in header! I changed it "-(float)getNumber" and it works!! thanks!!
Everything is OK. You'll receive 3.23