1 registered members (TedMar),
1,420
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Call function from a function
#411310
11/15/12 13:53
11/15/12 13:53
|
Joined: Feb 2006
Posts: 33 Herts, UK
danohu
OP
Newbie
|
OP
Newbie
Joined: Feb 2006
Posts: 33
Herts, UK
|
Hi In Workshop 3 the following code is provided which calls a function 'compute' from function main(). I want to be able to call a function from a function other than main e.g. replace function main () with something like function resp1 (). When I do this with the code below and call the 'compute' function it will not work. It would seem that you can only call another function from function main (). Would this be correct and if not how do I achieve this? This is the original code from Workshop 3:
////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>
var my_age = 33;
var days_a_year = 365;
var number_of_days; // we need to calculate this value
////////////////////////////////////////////////////////////////////
PANEL* pDisplay =
{
digits (10, 10, 5, *, 1, number_of_days);
flags = SHOW;
}
/////////////////////////////////////////////////////////////////////
function compute_days()
{
number_of_days = my_age * days_a_year;
}
function main()
{
screen_color.blue = 150;
compute_days();
}
If I change the code to what is below it does not work:
////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>
var my_age = 33;
var days_a_year = 365;
var number_of_days; // we need to calculate this value
function resp1();
////////////////////////////////////////////////////////////////////
PANEL* pDisplay =
{
digits (10, 10, 5, *, 1, number_of_days);
flags = SHOW;
}
/////////////////////////////////////////////////////////////////////
function compute_days()
{
number_of_days = my_age * days_a_year;
}
function resp1()
{
screen_color.blue = 150;
compute_days();
}
Thanks for your help
Last edited by JustSid; 11/15/12 14:35. Reason: Added code tags
|
|
|
Re: Call function from a function
[Re: danohu]
#411318
11/15/12 14:11
11/15/12 14:11
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Well I'm not sure if I understood what you really want but: The function ' main' is the only function that gets called automatically on startup - which means that renaming this function makes nothing happen on startup. So it - of course - is possible to call functions from other functions than main. and if you're posting code in future please use [ code] at the beginning and [ /code] at the end of your code. Much easier to read then
Last edited by Kartoffel; 11/15/12 14:32.
POTATO-MAN saves the day! - Random
|
|
|
Re: Call function from a function
[Re: danohu]
#411322
11/15/12 14:33
11/15/12 14:33
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
It would seem that you can only call another function from function main (). Would this be correct and if not how do I achieve this? Almost correct. There is another possibility. You can also append "_startup" to a function name. In this case the function will be called automatically right after the main function. If you got multiple startup functions they get called in the order they are defined in as far as I remember.
function my_startup()
{
printf("I started automatically");
}
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Call function from a function
[Re: Uhrwerk]
#411513
11/17/12 14:53
11/17/12 14:53
|
Joined: Nov 2011
Posts: 139 India
Yashas
Member
|
Member
Joined: Nov 2011
Posts: 139
India
|
Uhrwerk ,Thanks, I never knew that appending _startup to a function name starts automatically If I create multiple function as _startup. 1. Is the main executed first or the _startup?? 2. How would I know which function having _startup wud be executed first?? Thanks once again
|
|
|
Re: Call function from a function
[Re: Yashas]
#411519
11/17/12 14:59
11/17/12 14:59
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
1. Yes 2. AFAIK it's in the order they are defined in. Example:
#include <acknex.h>
void b_startup()
{
printf("B");
}
void main()
{
printf("Main");
}
void a_startup()
{
printf("A");
}
This sample should show message boxes in the order "Main", "B", "A" For detailed documentation: http://www.conitec.net/beta/askript-befehl.htm
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Call function from a function
[Re: Yashas]
#411522
11/17/12 15:01
11/17/12 15:01
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
How would I know which function having _startup wud be executed first If you got multiple startup functions they get called in the order they are defined in as far as I remember regarding 1: test it place error("something to identify the function"); in the first line of a _startup-function and in the first line of main() The error which appears first belongs to the function that gets called first: EDIT: and again... Uhrwerk was faster
Last edited by Kartoffel; 11/17/12 15:01.
POTATO-MAN saves the day! - Random
|
|
|
Re: Call function from a function
[Re: Yashas]
#411528
11/17/12 15:12
11/17/12 15:12
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
If you're lazy you don't even need printf. All automatically started functions are logged in acklog.txt.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|