Posts

Showing posts with the label function

How can I pass in parameters without calling a function in Python? [duplicate]

Image
Clash Royale CLAN TAG #URR8PPP How can I pass in parameters without calling a function in Python? [duplicate] This question already has an answer here: I am using the Tkinter bind function, and I would like to bind a button to a function. However, this function takes parameters (which, for obvious reasons, I have put in parentheses). encrypt_button = Button(window, text='Encrypt') encrypt_button.bind('<Button-1>', bl_encrypt(foo)) My problem is that, since I am passing 'foo' into my function, the function is called whenever the program gets to this line, not when I click on the button. This would not usually happen as, if there were no parameters to be passed, I would not put parentheses after the function name. How do I bind to this function and pass parameters, but without the function being called as soon as I run the program? My Python version is 3.6.0, if that helps. This question has been asked before and already has an answer. If those answe...

if else: else portion not returning output

Image
Clash Royale CLAN TAG #URR8PPP if else: else portion not returning output I am currently trying to cycle through a dataframe of integers and characters and change one value of each row, conditionally. For all rows that do not meet the conditions I would just like to add them back into a new dataframe filled with the modified rows. I've done this before with no trouble, but I feel as though I have been staring at this too long without any enlightenment. a<-data.frame(cbind(1,'a',2,'c',3,'d'), stringsAsFactors = F) b<-data.frame(cbind(1,'a',2,'c',3,'g'), stringsAsFactors = F) c<-data.frame(cbind(1,'f',4,'g',5,'h'), stringsAsFactors = F) x<-rbind(a,b,c) fun<-function(x){ fin<-NULL for(i in 1:nrow(x)){ v<-x[i+1,] if ((x[i,1]== v[i,1]) & (x[i,2]==v[i,2]) ){ x[i,3]<-"f" fin<-rbind(fin, x[i,]) }else {fin<-rbind(fin, x[i,]) } return(fin) } } fun(...

Sum Function with array argument

Image
Clash Royale CLAN TAG #URR8PPP Sum Function with array argument i'm starting studying javascript. I'm stuck with this exercise: Create a function that takes an array as argument and return the sum of all elements of the array. I have written this code: function sum(table) { let x = table[0]; let y = [table.length - 1]; let totale = 0; for (count = x; count <= y; count++) { total += x; x += 1; }; return total; }; console.log(sum[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); I don't know why the result is undefined instead of 55. Thanks for helping. let y = [table.length - 1]; I don't think you want y to be an array. – CertainPerformance 13 mins ago let y = [table.length - 1]; y Your variable is called totale , not total . And you want to iterate from 0 , not from table[0] . ...

How to find the type of the caller of a function in Rust?

Image
Clash Royale CLAN TAG #URR8PPP How to find the type of the caller of a function in Rust? How can I get the caller type in my function? struct A; struct B; impl A { fn new() -> Self { A } fn call_function(&self) { B::my_function(); } } impl B { pub fn my_function() { println!("Hello"); // println!("{}" type_of_the_caller) // I want to get type A here // Is it possible to get the caller type which is A in this case? } } fn main() { let a = A::new(); a.call_function(); } Here is the working code in playground. This is simplified code for an example. I guess you meant fn call_function(&self) ? Please add more precision about what you want to do with the type of the caller – Boiethios 5 hours ago fn call_function(&self) ...

What does “[*]” (star modifier) mean in C? [duplicate]

Image
Clash Royale CLAN TAG #URR8PPP What does “[*]” (star modifier) mean in C? [duplicate] This question already has an answer here: While trying to implement a C11 parser (for educational purposes), I found that in C11 (p. 470) but also in C99 (p. 412) (thanks Johannes!), the direct declarator is defined as: (6.7.6) direct-declarator: direct-declarator [ type-qualifier-list? * ] At first, I thought this was an error in the grammar (the type list shouldn't be optional). However, when I tried this out in my reference compiler (clang), I got an rather unexpected error: int array[*] = { 1, 2, 3 }; // error: star modifier used outside of function prototype So apparently, (in clang) this is called the star modifier . I quickly learned that they can only be used in function signatures: void foobar(int array[*]) However, they can only be used in the declaration. Trying to use it in a function definition results in an error as well: void foobar(int array[*]) { // variable length array...

Error in function to create heatmap using ggplot2

Image
Clash Royale CLAN TAG #URR8PPP Error in function to create heatmap using ggplot2 I'm trying to make a heatmap using ggplot2. What I want to be plotted is in the form of a matrix which is the result of a function. Here is the data: Here is the function: vector_a <- names(master)[2:4] vector_b <- names(master)[5:6] heatmap_prep <- function(dataframe, vector_a,vector_b){ dummy <- as.data.frame(matrix(0, nrow=length(vector_a), ncol=length(vector_b))) for (i in 1:length(vector_a)){ first_value <- dataframe[[ vector_a[i] ]] # print(first_value) for(j in 1:length(vector_b)){ second_value <- dataframe[[ vector_b[j] ]] result <- cor(first_value, second_value, method = "spearman") dummy [i,j] <- result } } rownames(dummy) <- vector_a return(as.matrix(dummy)) heatmap_data_matrix1 <- heatmap_prep(master,vector_a, vector_b) Using the data in h...

Defined function is undefined when called React-Native [on hold]

Image
Defined function is undefined when called React-Native [on hold] I'm currently working on an application in React-Native implementing Redux and Thunk and I stumbled across an error I can't figure out: My view container ( The one that connect the component view with a mapStateTopProps and a mapDispatchToProps ) set a method named 'getAllEvents()' in my component props. Even if the method is defined as shown on the screenshot below, I get an undefined whenever I call it... this.props.getAllEvents => [Function getAllEvents] this.props.getAllEvents() => undefined How can a defined function could return undefined when it is called? This question appears to be off-topic. The users who voted to close gave this specific reason: stackoverflow.com/help/how-to-ask – Zyigh 22 mins ago thanks, I've e...

Can php skip part of my function by some (server) error?

Can php skip part of my function by some (server) error? I have searched the site for a question that is the same or similar, but I cannot find what I am looking for. Perhaps this is because my question is somewhat strange or unlogical (but I hope you'll be able to clear that up). Here's the case: I have a function to test to see if something is not in an array. I have to make absolutely sure this function returns only true if what I look for is not in the array. I have recreated a simplified function so you understand what I mean. In this function I want to make sure there is not any 'A' in my array, one or more. 'A' $array = ["A", "F", "B", "G"] function check($array) { $noAs = false; $n = 0; foreach ($array as $letter) { if ($letter == "A") { $n++; } } if ($n == 0) { $noAs = true; }; return $noAs; } I then can show the result: if (check($array) =...

How to make a seperate function to print output in C#?

How to make a seperate function to print output in C#? I am learning function's in C# & decided to make a calculator. Here is the code I have written:- using System; namespace Second_Project { class Program { static void Main(string args) { Console.WriteLine("Entet any two numbers to find out its sum, product, difference, division & remaindern"); Console.WriteLine("Enter first number:-"); string input1 = Console.ReadLine(); Console.WriteLine("Enter second number:-"); string input2 = Console.ReadLine(); int num1 = int.Parse(input1); int num2 = int.Parse(input2); Calculation(num1,num2); Console.Read(); } private static void Calculation(float n, float m) { float sum, sub, mul, div, mod; sum ...