Calling a function with no arguments (2024)

45 views (last 30 days)

Show older comments

Swati Sarangi on 13 Nov 2020

  • Link

    Direct link to this question

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments

  • Link

    Direct link to this question

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments

Answered: Steven Lord on 6 Aug 2024 at 14:59

Hi All,

In my program , I'm trying to call a function with some input arguments but no outputs as I'm using this function for the purpose of plotting.

How should its syntax look like?

[ ] = function_name( var1,var2,var3) // This line is giving me erorr : Assigning an output to an empty array is not supported.

Please suggest me a way to handle this error.

Thanks in advance!

4 Comments

Show 2 older commentsHide 2 older comments

Rik on 13 Nov 2020

Direct link to this comment

https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_1130823

  • Link

    Direct link to this comment

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_1130823

If you're using a function to plot, you can return the handles to the graphics object, just like the plot function does.

John Wolter on 6 Aug 2024 at 14:28

Direct link to this comment

https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_3231196

  • Link

    Direct link to this comment

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_3231196

Edited: John Wolter on 6 Aug 2024 at 14:55

The title of this question could be clearer. The function has input arguments but no output arguments. It's a good question, just not the one I was searching for.

Torsten on 6 Aug 2024 at 14:33

Direct link to this comment

https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_3231206

  • Link

    Direct link to this comment

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_3231206

Open in MATLAB Online

@John Wolter

So you searched for:

[a b c] = fun()

a = 1

b = 2

c = 3

function [a b c] = fun()

a = 1;

b = 2;

c = 3;

end

?

John Wolter on 6 Aug 2024 at 14:53

Direct link to this comment

https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_3231221

  • Link

    Direct link to this comment

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_3231221

More or less, yes. My issue is that I am trying to code in Matlab with a head full of Python knowledge. Simple questions like, "If I have no input arguments, do I need the parentheses after the function?" often have different answers in Matlab.

I'm going to edit my comment because technically a function output is considered an "output argument" in Matlab, another difference from Python. :-)

Sign in to comment.

Sign in to answer this question.

Answers (2)

Steven Lord on 6 Aug 2024 at 14:59

  • Link

    Direct link to this answer

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#answer_1495456

  • Link

    Direct link to this answer

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#answer_1495456

Outputs but no inputs

To call a function with outputs but no inputs, you can omit the parentheses or just include the parentheses with nothing inside them. If you were calling a function handle with outputs but no inputs, you would need to include the parentheses with nothing inside them. Otherwise MATLAB thinks you're trying to assign that variable to one or more variables and that may not work.

P = pi % one output, zero inputs to the pi function

P = 3.1416

P = pi() % one output, zero inputs

P = 3.1416

f = @pi;

P = f() % This works

P = 3.1416

P = f % P is not the value of pi but the function handle itself

P = function_handle with value:

@pi

try % Need to try/catch so I can run more code later in this answer

f = @computer;

[computerType, maxArraySize] = f % This does not work

catch ME

fprintf("This call threw error '%s'.\n", ME.message)

end

This call threw error 'Insufficient number of outputs from right hand side of equal sign to satisfy assignment.'.

Inputs but no outputs

To call a function with inputs but no outputs, just don't specify the square brackets and the equals sign.

help('pi') % zero outputs, one input to the help function

PI 3.1415926535897.... PI = 4*atan(1) = imag(log(-1)) = 3.1415926535897.... Documentation for pi doc pi

Neither inputs nor outputs

To call a function with neither inputs nor outputs, just type its name. Or you could use the empty parentheses if necessary. Some functions may assign to ans while others will not.

clear all

whos % No variables in the workspace

pi % zero outputs, zero inputs, assigns to ans

ans = 3.1416

whos % The pi call added ans to the workspace

Name Size Bytes Class Attributes ans 1x1 8 double

clear ans % ans no longer exists

why() % zero outputs, zero inputs, displays but does not assign to ans

Penny suggested it.

whos % And nothing shows up

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Mario Malic on 13 Nov 2020

  • Link

    Direct link to this answer

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#answer_543303

  • Link

    Direct link to this answer

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#answer_543303

Edited: Mario Malic on 13 Nov 2020

Open in MATLAB Online

You can achieve it this way.

function function_name( var1,var2,var3)

However, imagine a situation where you'd like to edit something on the plot that you've made, then you'd have to find the handles of it which is not a problem if you have only one figure/axes, but for multiples, you could return a figure handle.

function hFigure = function_name( var1,var2,var3)

hFigure = figure(1); % if you're using it

plot(hFigure, x, y);

end

2 Comments

Show NoneHide None

Stephen23 on 13 Nov 2020

Direct link to this comment

https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_1130798

  • Link

    Direct link to this comment

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_1130798

Open in MATLAB Online

@Mario Malic: are you sure about that function definition syntax? The syntax given in the documentation is:

function hFigure = function_name(var1,var2,var3)

Mario Malic on 13 Nov 2020

Direct link to this comment

https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_1130813

  • Link

    Direct link to this comment

    https://ms-intl.mathworks.com/matlabcentral/answers/646553-calling-a-function-with-no-arguments#comment_1130813

Edited: Mario Malic on 13 Nov 2020

I haven't had my first coffee yet! I edited the answer, thanks Stephen.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsPrinting and Saving

Find more on Printing and Saving in Help Center and File Exchange

Tags

  • function

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Calling a function with no arguments (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Calling a function with no arguments (2024)

References

Top Articles
MyChart Frequently Asked Questions | Cleveland Clinic
20 Funniest Big Lebowski Quotes, Ranked
Www.mytotalrewards/Rtx
Netronline Taxes
Devon Lannigan Obituary
Ghosted Imdb Parents Guide
Beacon Schnider
Craigslist Furniture Bedroom Set
Noaa Weather Philadelphia
How to Watch Braves vs. Dodgers: TV Channel & Live Stream - September 15
Jet Ski Rental Conneaut Lake Pa
William Spencer Funeral Home Portland Indiana
Love Compatibility Test / Calculator by Horoscope | MyAstrology
Chicken Coop Havelock Nc
Calmspirits Clapper
Tamilrockers Movies 2023 Download
Billionaire Ken Griffin Doesn’t Like His Portrayal In GameStop Movie ‘Dumb Money,’ So He’s Throwing A Tantrum: Report
Velocity. The Revolutionary Way to Measure in Scrum
Average Salary in Philippines in 2024 - Timeular
EASYfelt Plafondeiland
Rugged Gentleman Barber Shop Martinsburg Wv
Costco Jobs San Diego
TMO GRC Fortworth TX | T-Mobile Community
Cosas Aesthetic Para Decorar Tu Cuarto Para Imprimir
Ts Modesto
Meowiarty Puzzle
Google Flights To Orlando
Everything You Need to Know About Ñ in Spanish | FluentU Spanish Blog
The Rise of "t33n leaks": Understanding the Impact and Implications - The Digital Weekly
Does Circle K Sell Elf Bars
Spy School Secrets - Canada's History
Beaver Saddle Ark
Truckers Report Forums
Kips Sunshine Kwik Lube
Ukg Dimensions Urmc
My.lifeway.come/Redeem
Bismarck Mandan Mugshots
Ticket To Paradise Showtimes Near Regal Citrus Park
Craigslist Ludington Michigan
Adam Bartley Net Worth
Indiana Jones 5 Showtimes Near Cinemark Stroud Mall And Xd
Henry Ford’s Greatest Achievements and Inventions - World History Edu
Ross Dress For Less Hiring Near Me
“To be able to” and “to be allowed to” – Ersatzformen von “can” | sofatutor.com
Who Is Responsible for Writing Obituaries After Death? | Pottstown Funeral Home & Crematory
Hazel Moore Boobpedia
Walgreens On Secor And Alexis
Martha's Vineyard – Travel guide at Wikivoyage
Aloha Kitchen Florence Menu
Hsi Delphi Forum
Craigslist Yard Sales In Murrells Inlet
Craigslist Centre Alabama
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6232

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.