Call function from a function

Posted By: danohu

Call function from a function - 11/15/12 13:53

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:

Code:
////////////////////////////////////////////////////////////////////
#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:



Code:
////////////////////////////////////////////////////////////////////
#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
Posted By: Kartoffel

Re: Call function from a function - 11/15/12 14:11

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 wink
Posted By: Uhrwerk

Re: Call function from a function - 11/15/12 14:33

Originally Posted By: danohu
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.
Code:
function my_startup()
{
   printf("I started automatically");
}

Posted By: Yashas

Re: Call function from a function - 11/17/12 14:53

Uhrwerk ,Thanks, I never knew that appending _startup to a function name starts automatically laugh

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 laugh
Posted By: Uhrwerk

Re: Call function from a function - 11/17/12 14:59

1. Yes
2. AFAIK it's in the order they are defined in.

Example:
Code:
#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
Posted By: Yashas

Re: Call function from a function - 11/17/12 15:01

Thank You very much laugh
Posted By: Kartoffel

Re: Call function from a function - 11/17/12 15:01

Originally Posted By: Yashas
How would I know which function having _startup wud be executed first
Originally Posted By: Uhrwerk
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 grin
Posted By: Yashas

Re: Call function from a function - 11/17/12 15:05

^ grin Thank You both
Posted By: Uhrwerk

Re: Call function from a function - 11/17/12 15:12

If you're lazy you don't even need printf. All automatically started functions are logged in acklog.txt.
© 2024 lite-C Forums