Calling function with no input argument

Click For Summary

Discussion Overview

The discussion revolves around issues related to defining and calling a function in MATLAB that does not take any input arguments and returns one output. Participants explore potential mistakes in the function definition and calling syntax.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their function definition and calling method, expressing confusion about why the program halts.
  • Another participant suggests checking for a file named sortabc.m, MATLAB's case sensitivity, and whether square brackets are necessary for output arguments.
  • A different participant critiques the original function call, questioning the use of the variable name sumout and suggesting that the function's return value should be assigned differently.
  • Another participant claims that parentheses should not be used when calling a function with no inputs and proposes an alternative syntax for the function definition.

Areas of Agreement / Disagreement

Participants express differing views on the correct syntax for calling the function and whether parentheses are necessary. There is no consensus on the best approach, as multiple competing perspectives are presented.

Contextual Notes

Some participants reference potential misunderstandings related to MATLAB's syntax rules and function definitions, but these remain unresolved within the discussion.

mushi
Messages
23
Reaction score
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 separately. 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
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?
 
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 separately. 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 :-p
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.
 
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.
 

Similar threads

  • · Replies 54 ·
2
Replies
54
Views
7K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 26 ·
Replies
26
Views
3K
Replies
5
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K