473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

functions and not so global variables.

Hi,

I want to tidy my code up a bit and in order to do that I need to break some
function down.

so if I have something like

function function_1()
{
$foo = 0;
$bar = 1;

$foobar = $foo + $foo
echo $foobar;
}

and I want to split it in two functions I would maybe get

function function_2()
{
$foobar = $foo + $foo
echo $foobar;
}

function function_1()
{
$foo = 0;
$bar = 1;

function function_2()
}

But of course the problem is that function_2 does not know what $foo and
$bar are equal to because the values are not global.
But how could I make function_2 know about the values defined in
function_1()?

The purpose is just to clean up the code so I do not want to do anything
that might be a security problem.

Many thanks in advance.

Sims

Jul 17 '05 #1
2 1411
Sims wrote:
I want to tidy my code up a bit and in order to do that I need to break some
function down and I want to split it in two functions
But of course the problem is that function_2 does not know what $foo and
$bar are equal to because the values are not global.
But how could I make function_2 know about the values defined in
function_1()?


function function_2($foo,$bar) {
$foobar = $foo + $foo
echo $foobar;
}

function function_1() {
$foo = 0;
$bar = 1;

function function_2($foo,$bar)
}

Or if you want to be able to use default values:
function function_2($foo=1,$bar=0) {
$foobar = $foo + $foo
echo $foobar;
}

function function_1() {
$foo = 0;
$bar = 1;

function function_2($foo,$bar)
}

function function_3() {
/* can call the function_2() without arguments
and foo will be 1 and bar would be 0 */
function function_2()
}
//Aho
Jul 17 '05 #2
Sims a écrit:
Hi,

[...]
But of course the problem is that function_2 does not know what $foo and
$bar are equal to because the values are not global.
But how could I make function_2 know about the values defined in
function_1()? Just 'pass' the values as args to function_2 : function
function_2($foo,$bar)

Many thanks in advance.

For nothing

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: will taubin | last post by:
i would like my program to have a config.php with passwords and other stuff. i would like to have a functions.php to hold all my functions. i would like the functions.php to include/require...
7
by: Angelos | last post by:
hello there... I would like to ask if there is any way of using variables which are outside functions in the functions without passing them when I call the function... Complicated eehh ? I have...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
76
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
10
by: ankisharma | last post by:
Hi all At many places I have seen that programmers pass global variables to functions in c. I am not able to figure out why they do so. need some clues on this. somewhere i heard that this...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
5
by: jonkersbart | last post by:
Dear, I have wrote a script and want to group some functions of the script in a separate modulo so that I can import the module in other scripts and use the same functions there.. The problem...
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.