myfunc<-function(x){
a=0
b=1
print(a)
print(b)
for(x in 1:8){
c=sum(a+b)
print(c)
a=b
b=c
}}
myfunc(x)
Please explain the use of myfunc(x) here?
myfunc<-function(x){
a=0
b=1
print(a)
print(b)
for(x in 1:8){
c=sum(a+b)
print(c)
a=b
b=c
}}
myfunc(x)
Please explain the use of myfunc(x) here?
the answer is it has no use, serves no purpose, and can be omitted.
myfunc<-function(){
a=0
b=1
print(a)
print(b)
for(x in 1:8){
c=sum(a+b)
print(c)
a=b
b=c
}}
myfunc()
however that reveales that the code is hardcoded, so will always give the same result, it is not parameterised in any way, typically in a function you want to pass an argument to get a different behaviour.
like this
myfunc<-function(iterations){
a=0
b=1
print(a)
print(b)
for(i in 1:iterations){
c=sum(a+b)
print(c)
a=b
b=c
}}
myfunc(1)
myfunc(2)
myfunc(10)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.