/    Sign up×
Community /Pin to ProfileBookmark

Functions that call eachother

Hello guys, I am looking for someone to explain me something since I can’t find a clear answer. My question is about functions that call themselves. I saw that it is possible to build a list of functions that are ‘chained’ together and for example the first function calls the second one then the second one calls another one. My confusion is : Lets say that you have a second function that has a variable` let a = 12;` if i call that function on the first function, will i have access to that variable or whatever that second function might have inside? How can i pass info to another function? Thanks in advance guys.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@VITSUSAAug 28.2020 — What you want to do exactly? After that we can suggest you best possible answer to do this task easily.
Copy linkTweet thisAlerts:
@NogDogAug 28.2020 — > @klajdi#1622659 My question is about functions that call themselves.

> @klajdi#1622659 build a list of functions that are 'chained' together


Sounds like two different things? When a function calls itself, it's referred to a _recursion_, or a _recursive function_.

In either case, though, a function (normally) only has access to things from outside of it if they are passed in as function parameters; so if you need access to something from another function, then either you need to call that function and it needs to return what you're looking for, or if the data is going the other way, you need to pass the data of interest into the newly called function via a parameter (a.k.a. argument).
Copy linkTweet thisAlerts:
@klajdiauthorAug 28.2020 — @NogDog#1622677 could you give me an example of what you said about passing data via parameters please? This is what exactly what I meant.
Copy linkTweet thisAlerts:
@NogDogAug 28.2020 — In this rather silly (and untested) example, some_text is the function parameter, and when I recursively call the function, I pass it (with modification) into that internal call so that it's now available in that instance of the function:
<i>
</i>function my_func(some_text) {
if(some_text.length &gt;= 10) {
return some_text;
}
some_text = some_text + "x";
return(my_func(some_text));
}
×

Success!

Help @klajdi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...