argument-passing

Function arguments (in Python for example)

北战南征 提交于 2019-11-27 23:24:02
What are [function] arguments? What are they used for? I started learning Python very recently; I'm new to programming and I apologize for this basic question. In every Python tutorial I go through they talk about arguments . I have looked for the answer to this question and have found many answers but they are just a little too hard for me to understatnd. I may just be missing some conceptual background. So... when I define a function, what are the things in parenthesis used for? Example: def hi( This is the part that i dont get): print 'hi' Edit: Two follow-up questions related to this one

Functions with a flexible list of ordered/unordered and labeled/unlabeled inputs in MATLAB

烈酒焚心 提交于 2019-11-27 23:16:59
A lot of MATLAB functions have an input structure such as: output = function MyFun(a,b,c,'-setting1',s1,'-setting2',s2,'-setting3',s3) I am wondering how I should implement this kind of functionality in my own functions. To be precise, I would like to find out how I can create a function such that: The function has a variable number of inputs N + M The first N inputs are ordered and unlabeled . In the example above, N = 3 . The first input is always a , second input is always b , third input is always c . The function input is variable in that users do not necessarily need to send b , c ; when

Postgres integer arrays as parameters?

牧云@^-^@ 提交于 2019-11-27 20:04:26
问题 I understand that in Postgres pure, you can pass an integer array into a function but that this isn't supported in the .NET data provider Npgsql. I currently have a DbCommand into which I load a call to a stored proc, add in a parameter and execute scalar to get back an Id to populate an object with. This now needs to take n integers as arguments. These are used to create child records linking the newly created record by it's id to the integer arguments. Ideally I'd rather not have to make

Function pointer as an argument

帅比萌擦擦* 提交于 2019-11-27 17:42:30
Is it possible to pass a function pointer as an argument to a function in C? If so, how would I declare and define a function which takes a function pointer as an argument? Mehrdad Afshari Definitely. void f(void (*a)()) { a(); } void test() { printf("hello world\n"); } int main() { f(&test); return 0; } Michal Sznajder Let say you have function int func(int a, float b); So pointer to it will be int (*func_pointer)(int, float); So than you could use it like this func_pointer = func; (*func_pointer)(1, 1.0); /*below also works*/ func_pointer(1, 1.0); To avoid specifying full pointer type every

Check inside method whether some optional argument was passed

我只是一个虾纸丫 提交于 2019-11-27 15:33:17
How do I check if an optional argument was passed to a method? public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { if (optionalint was passed) return; } Another approach is to use Nullable<T>.HasValue ( MSDN definitions , MSDN examples ): int default_optionalint = 0; public void ExampleMethod(int required, int? optionalint, string optionalstr = "default string") { int _optionalint = optionalint ?? default_optionalint; } Well, arguments are always passed. Default parameter values just ensure that the user doesn't have to explicitly specify them

Passing an instance method as argument in PHP

巧了我就是萌 提交于 2019-11-27 14:57:53
I would like to create a Listener class class Listener { var $listeners = array(); public function add(callable $function) { $this->listeners[] = $function; } public function fire() { foreach($this->listeners as $function) { call_user_func($function); } } } class Foo { public function __construct($listener) { $listener->add($this->bar); } public function bar() { echo 'bar'; } } $listener = new Listener(); $foo = new Foo($listener); But this code fails with this error: Notice: Undefined property: Foo::$bar in index.php on line 18 Catchable fatal error: Argument 1 passed to Listener::add() must

Trouble passing on an argument to function within own function

不羁岁月 提交于 2019-11-27 14:33:02
I am writing a function in which I want to pass some arguments to the crrstep-function ('crrstep' package), but I encountered a problem: somehow the argument 'event' in my function is not recognized when I enter it in crrstep. I guess that crrstep looks in a different environment than the one I want it to look, but even after hours of searching for solutions on the web, I cannot seem to figure out how to solve this (I am quite inexperienced in programming..). Any help would be greatly appreciated! Here is some simulation data (adjusted example from crrstep-documentation) and an example of my

Argument passing strategy - environment variables vs. command line

一世执手 提交于 2019-11-27 11:05:20
Most of the applications we developers write need to be externally parametrized at startup. We pass file paths, pipe names, TCP/IP addresses etc. So far I've been using command line to pass these to the appplication being launched. I had to parse the command line in main and direct the arguments to where they're needed, which is of course a good design , but is hard to maintain for a large number of arguments. Recently I've decided to use the environment variables mechanism. They are global and accessible from anywhere, which is less elegant from architectural point of view, but limits the

Argparse - do not catch positional arguments with `nargs`.

霸气de小男生 提交于 2019-11-27 09:15:21
I am trying to write a function wo which you can parse a variable amount of arguments via argparse - I know I can do this via nargs="+" . Sadly, the way argparse help works (and the way people generally write arguments in the CLI) puts the positional arguments last. This leads to my positional argument being caught as part of the optional arguments. #!/usr/bin/python import argparse parser = argparse.ArgumentParser() parser.add_argument("positional", help="my positional arg", type=int) parser.add_argument("-o", "--optional", help="my optional arg", nargs='+', type=float) args = parser.parse

How to convert a command-line argument to int?

浪子不回头ぞ 提交于 2019-11-27 07:32:33
I need to get an argument and convert it to an int. Here is my code so far: #include <iostream> using namespace std; int main(int argc,int argvx[]) { int i=1; int answer = 23; int temp; // decode arguments if(argc < 2) { printf("You must provide at least one argument\n"); exit(0); } // Convert it to an int here } Since this answer was somehow accepted and thus will appear at the top, although it's not the best, I've improved it based on the other answers and the comments. The C way; simplest, but will treat any invalid number as 0: #include <cstdlib> int x = atoi(argv[1]); The C way with input