function-parameter


Passing functions as arguments in C++

回眸只為那壹抹淺笑 提交于 2020-02-03 08:10:44
问题 I'm a bit rusty in C++ and I'm switching after a year of python. Naturally I would like to translate the laziness coming from python to C++. I just discovered rot13 and I'm all excited about it. I found 3 ways of doing it and I wanted to do a little performance test. I wanted to see also if there is a difference in modifying the string in place or creating a new one. So I ended up having 6 functions. In the first method, I use a std::map to map the characters, thus I've built a class that

Understanding difference between int literal vs int parameter in PL/pgSQL function

妖精的绣舞 提交于 2020-01-24 20:42:23
问题 I have a function to left pad bit stings in PostgreSQL 9.5: CREATE OR REPLACE FUNCTION lpad_bits(val bit varying) RETURNS bit varying as $BODY$ BEGIN return val::bit(32) >> (32-length(val)); END; $BODY$ LANGUAGE plpgsql IMMUTABLE; which works fine: # select lpad_bits(b'1001100111000'); lpad_bits ---------------------------------- 00000000000000000001001100111000 (1 row) My problem is when I try to add a parameter to change the amount of padding: CREATE OR REPLACE FUNCTION lpad_bits(val bit

Using pointers or references to struct

霸气de小男生 提交于 2020-01-07 02:25:13
问题 Me and my friend is using structs in our code (our code is separate from each other). Lets take the following example: struct Book { char title; int pages; } void setBook(struct Book *tempBook) { tempBook->title = "NiceTitle"; tempBook->pages = 50; } The above code is pretty straight forward. The thing is, is there any difference in having these two mains: int main() { struct book obj1; setBook(&obj); } Or int main() { struct Book *obj2; setBook(obj2); } EDIT: I was not clear in my statement.

Error: Initializing Argument 1 of [closed]

偶尔善良 提交于 2020-01-06 14:47:34
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I've looked around and seen quite a few of these, but none of them provided the solution for my problem. I'm getting this compilation error with the following code: THE ERROR: THE CODE: const int TOP_WORDS = 25; ... void topWords(Hash t, string word, string topA[]); int main() { ... Hash table1; string word =

Error: Initializing Argument 1 of [closed]

*爱你&永不变心* 提交于 2020-01-06 14:47:27
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I've looked around and seen quite a few of these, but none of them provided the solution for my problem. I'm getting this compilation error with the following code: THE ERROR: THE CODE: const int TOP_WORDS = 25; ... void topWords(Hash t, string word, string topA[]); int main() { ... Hash table1; string word =

Declaring parameter of a function as final: why and when is it needed?

六月ゝ 毕业季﹏ 提交于 2020-01-03 15:56:37
问题 Going through a tutorial for android (related to multithreading, loopers and handlers), i came across this: public synchronized void enqueueDownload(final DownloadTask task) My questions are: When and why is it needed to declare a parameter of a function as final? Is it specific to Java or does something similar exist in other languages such as C/C++? 回答1: In Java this is usually so that you can access the parameter within an anonymous inner class - which is often used in Android for event

What's this usage of the variable cast to void in function body?

一世执手 提交于 2020-01-03 06:51:32
问题 I am just learning to code embedded C. I see some code like below. The function is defined like this: void printDebug(const char d1[]){(void)d1;} And it is used like this: printDebug("BLE_UART_EVENT"); I don't understand its purpose. It gives me an impression of a callable char array? 回答1: It's not calling char array, it's just explicitly converting the char array to void . (And the evaluated result is discarded immediately.) I think it's just used to prohibit the compiler warning of unused

Variadic templates without function parameters

你。 提交于 2020-01-02 02:01:08
问题 Can I use variadic templates without using the template parameters as function parameters? When I use them, it compiles: #include <iostream> using namespace std; template<class First> void print(First first) { cout << 1 << endl; } template<class First, class ... Rest> void print(First first, Rest ...rest) { cout << 1 << endl; print<Rest...>(rest...); } int main() { print<int,int,int>(1,2,3); } But when I don't use them, it doesn't compile and complains about an ambiguity: #include <iostream>

How to deal with name/value pairs of function arguments in MATLAB

瘦欲@ 提交于 2019-12-27 18:20:56
问题 I have a function that takes optional arguments as name/value pairs. function example(varargin) % Lots of set up stuff vargs = varargin; nargs = length(vargs); names = vargs(1:2:nargs); values = vargs(2:2:nargs); validnames = {'foo', 'bar', 'baz'}; for name = names validatestring(name{:}, validnames); end % Do something ... foo = strmatch('foo', names); disp(values(foo)) end example('foo', 1:10, 'bar', 'qwerty') It seems that there is a lot of effort involved in extracting the appropriate

How to create a polyvariadic haskell function?

妖精的绣舞 提交于 2019-12-27 11:08:27
问题 I need a function which takes an arbitrary number of arguments (All of the same type), does something with them and afterwards gives a result back. A list of arguments is impracticable in my specific case. As I looked through the haskell libs, I saw that the function printf (from module Text.Printf ) uses a similar trick. Unfortunately, I couldn't understand that magic by looking at the source. Can somebody explain how to achieve this, or at least some webpage/paper/whatever where I could

工具导航Map