Calling function with no input argument

In summary: Since you are assigning the return value from sortabc to the sumout variable, then the names do overlap. In this case, you would want to prefix sumout with an underscore (e.g. "_sumout"), so that it is more easily distinguished from the other variables in the program.
  • #1
mushi
23
0
Hi guys,

I am going to ask another very stupid question. But surprisingly, I could not find any help on it on internet. My question is:

I have defined a function which doesn't take any any input and one output. When I called it, the program halts. I have checked the function and it is working fine seperately. My be i am making mistake in calling it. Her is how I defined it:

function [sumout] = sortabc()
...
end

Here is how I am calling it:
sumout = sortabc();


Can anybody kindly tell me if I am making any mistake in calling ir defining the function sortabc.

Regards.
 
Engineering news on Phys.org
  • #2
I guess you are talking Matlab, right?

I don't use matlab, but I would like to think that I am a good detective since I do fine in my programming and debugging. So, here are some stupid suggestions, not knowing MATLAB and simply having done a google on function definitions in matlab:

do you have a file named sortabc.m ?

is MATLAB case sensitive?

what does the file name look like from the file system?

are you working on Windows or Linux?

do you need to enclose the output argument in square brackets? like
[sumout] = sortabc()
or is that only needed when you have more than one argument?

Otherwise, do what I refer to "baby steps" ...go back and define a trivial function that should work, like one that does nothing important but returns, say 9... and go from there...if that does not work, maybe something along what I asked above is happening?
 
  • #3
mushi said:
I have defined a function which doesn't take any any input and one output. When I called it, the program halts. I have checked the function and it is working fine seperately. My be i am making mistake in calling it. Her is how I defined it:

function [sumout] = sortabc()
...
end

Here is how I am calling it:
sumout = sortabc();Can anybody kindly tell me if I am making any mistake in calling ir defining the function sortabc.
The biggest mistake you are making is not having a manual or textbook in front of you and open at some pages of code, any code, where there is plenty of code statements for you to refer to. I suggest that you always do that.

You don't say what you are coding in. But based on my extensive coding experience :tongue2:
I can say what you wrote looks wrong or at least weird. (Though that's not to say it's not right.)
Code:
 Here is how I am calling it:
sumout = sortabc();
Okay, so you are assigning to a variable whatever is returned by your function sortabc.

Code:
function [sumout] = sortabc()
...
end
So I am perplexed to see this. I don't expect to see sumout mentioned in the function's definition, since I have already concluded sumout is a variable in your main program. And you seem to be telling the function to return the value of a call to function sortabc(). I'd expect to see you assign something to the function's name like:
Code:
fuction sortabc()
...
sortabc='thesortedstring'
end

As I said, I don't recognize what you are programming in. But if you open a manual or something filled with lots of code, flip through it and you will soon find a working example of what you are wanting to do.
 
  • #4
I had the same problem, and similarly could not find an answer online. But here it is:

Assuming you are using Matlab, you do not need the parentheses when you call the function. You defined it correctly (although you shouldn't need brackets):
function sumout = sortabc();

When you call a function with no inputs, do not use parentheses:
sumout = sortabc;

Using the same variable names shouldn't be a problem. Their domains don't overlap.
 
  • #5


Hello,

It seems like you are on the right track with your function definition and calling it. However, there may be a few things that could be causing the program to halt. Here are some possible suggestions to troubleshoot the issue:

1. Check if there are any errors in your function definition. Make sure that all the necessary inputs and outputs are properly defined and used within the function.

2. Ensure that the function is saved in the same directory as your main program. If the function is saved in a different directory, you may need to add that directory to your search path.

3. Check if there are any infinite loops within your function. This could also cause the program to halt.

4. Try running the function with a different set of inputs to see if it works. It is possible that the inputs you are using are causing an error within the function.

I hope this helps. Best of luck!
 

What is a function with no input argument?

A function with no input argument is a type of function in computer programming that does not require any input parameters or values to be passed in order to be executed. This means that the function will perform its designated task without needing any additional information from the user.

Why would you use a function with no input argument?

A function with no input argument can be useful in situations where you want to perform a specific task or operation without needing any specific input from the user. This can save time and make the code more efficient, especially if the function is used multiple times throughout the code.

How do you call a function with no input argument?

To call a function with no input argument, you simply write the name of the function followed by a pair of parentheses. For example, if your function is called "printMessage()", you would call it by writing "printMessage()" in your code.

What happens if you try to pass an argument to a function with no input argument?

If you try to pass an argument to a function with no input argument, you will likely receive an error message. This is because the function is not designed to accept any input values, so it will not know what to do with the argument you are trying to pass.

Can a function with no input argument return a value?

Yes, a function with no input argument can still return a value. The lack of input parameters does not affect the function's ability to perform operations and return a result. However, if the function does not require any input, it may not be necessary for it to return a value.

Similar threads

  • Set Theory, Logic, Probability, Statistics
2
Replies
54
Views
3K
Replies
36
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
2
Views
126
Replies
4
Views
1K
Replies
12
Views
1K
Replies
1
Views
654
  • Materials and Chemical Engineering
Replies
14
Views
2K
Replies
1
Views
1K
Back
Top