필터 지우기
필터 지우기

Briefly explain two main differences between writing MATLAB code as a user-defined function compared with just writing a script file?

조회 수: 3 (최근 30일)
So im not sure about this one. Is it to do with the different input streams of a script to a function?
Please help thanks!!

답변 (1개)

Stephen23
Stephen23 2015년 12월 6일
편집: Stephen23 2015년 12월 6일
Scripts are just a collection of commands, whereas:
  1. Functions each have their own workspace.
  2. Functions can have input and output arguments
  3. Functions make debugging easier
You should learn to write functions. As the documentation says "Program files can be scripts that simply execute a series of MATLAB® statements, or they can be functions that also accept input arguments and produce output... However, functions are more flexible and more easily extensible."
  • "Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace."
  • "Functions, which can accept input arguments and return output arguments. Internal variables are local to the function."
Scripts are great for testing something out. But if your code is going to hang around for more than a day, turn it into a function.
  댓글 수: 2
Jan
Jan 2015년 12월 6일
And: You can define subfunctions in functions files only, not in script files.
Walter Roberson
Walter Roberson 2015년 12월 6일
Historically, the Just In Time compiler worked only on functions; however lately it increasingly works on scripts as well.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by