scilab local variables on different functions -
so have i'm calling fun3 calls fun1. in fun1 user inputs text in variables , b fun2 makes utilize of it, how link local variables in different functions , bring them fun3 create utilize of them too, have define them in main programme first?
i know have utilize input , output arguments in functions think i'm doing wrong left them empty help me finish them
functions
function [] = fun1 () a=input("input text") b=input("input text") end function function [] = fun2() printf("a b") end function function [] = fun3() fun1() fun2() disp(a) disp(b) end function
main program
fun3()
welcome world of programming/scilab. there great tutorials such these can help started. find list of others on the scilab wiki.
to question let's go through each function 1-by-1.
fun1this function needs inquire user 2 strings , b. think meant inputs strings according the input function docs, should add together "string" call. see examples on link. create fun1 homecoming these string set them in output arguments of function.
function [a,b] = fun1() a=input("input text: ", "string") b=input("input b text: ", "string") endfunction
fun2 because used printf, i'm assuming print , b additional text. can see examples on using printf @ bottom of this page. print them, need know value, specify , b input parameters.
function [] = fun2(a,b) printf("a (%s) b (%s)", a, b) endfunction
fun3 in fun3 want phone call function user input called fun1 , whatever returns, want send fun2 prints two. want print values again.
notice when using functions, names not need same names of local variables.
function [] = fun3() [c,d] = fun1() fun2(c,d) disp(c) disp(d) endfunction
answer question defining variables in main programme makes them global. if can avoid this, avoid this. seek work local variables , utilize proper input , output variables showed above. way know every variable flowing in function , out of function within 1 glance. makes testing functions much easier. if know function works trying kinds of inputs , checking outputs, can forget , utilize it.
also see why globals bad. c, makes valid points.
finallyi suggest tutorials first, , understand basic stuff.
function scilab
No comments:
Post a Comment