Hello, and thank you for viewing this thread. I was just wondering if there was a way to have subroutines, like in BASIC, in C++ that can use all of the same variables that main() does.
5 3572
Hello, and thank you for viewing this thread. I was just wondering if there was a way to have subroutines, like in BASIC, in C++ that can use all of the same variables that main() does.
No you can't: local variables have lexical scoping, i.e. they aren't visible in
another function call. You can have global variables though which are visible
everywhere but are considered bad practice. An alternative would be to pass
references of the local variables to the called function.
Try to study a bit on object oriented programming and you'll see that you hardly
need global variables (Basic uses them a lot).
kind regards,
Jos
No you can't: local variables have lexical scoping, i.e. they aren't visible in
another function call. You can have global variables though which are visible
everywhere but are considered bad practice. An alternative would be to pass
references of the local variables to the called function.
Try to study a bit on object oriented programming and you'll see that you hardly
need global variables (Basic uses them a lot).
kind regards,
Jos
bad practice, eh?
well, I REALLY need this, so...how do you do global variables?
thanks
Though its a bad practice You can use Macros. but u have to make sure you dont use the macro any where else.
Hi ,
Since i am not aware of the precise context, I assume that you need all the other functions to use vatiables declared in main(). You can do so my moving the variables to a struct , declare the struct in main, and change those functions to take this struct as argument(pass by ref). I think should solve the problem.
Regards,
Prasannaa.
Hi ,
Since i am not aware of the precise context, I assume that you need all the other functions to use vatiables declared in main(). You can do so my moving the variables to a struct , declare the struct in main, and change those functions to take this struct as argument(pass by ref). I think should solve the problem.
Regards,
Prasannaa.
that sounds like a great solution
it would be fantasticif someone--anyone could post some code on how to do that
thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
by: joe |
last post by:
Hello:
I am using IIS 5.0, ASP and VBScript. I am puzzled by recurrences of errors
when I nest subroutines, and I'd like to know if there are rules for that
which I don't know. Would functions...
|
by: Microsoft |
last post by:
I'm not sure where to physically place my subroutines in vb.net I get
namespace and not declared errors...
Imports System
Imports System.Management
Public Class Form1
|
by: OpticTygre |
last post by:
I have a class, ProcessFiles, with several subroutines it runs for each type
of file I want to "process."
First, I check directories for files. Then, based on the filenames of those
I find in...
|
by: Victor |
last post by:
In ASP, if I define a subroutine as private
Private Sub mySubroutine(arg1, arg2)
I understand that the variables I define inside the subroutine are local only to the
subroutine. If the same...
|
by: Wijaya Edward |
last post by:
Hi,
How does one benchmark
multiple subroutines in Python?
Is there a built-in library for that?
--
Regards,
Edward WIJAYA
SINGAPORE
|
by: hanhaner |
last post by:
hey, there
I am new to Python and C++. Now I am trying to call some fortran subroutines in python code. I was told the good way is to write a c++ interface. I am not quite sure how to do it.
...
|
by: hanhaner |
last post by:
hey, there
I am new to Python and C++. Now I am trying to call some fortran subroutines in python code. I was told the good way is to write a c++ interface. I am not quite sure how to do it.
...
|
by: bbasberg |
last post by:
Hello,
First of all, I'm very sorry i mistakenly posted this message to the "articles" forum rather than this one and am unable to erase it from there. I apologize for the cross-posting.
...
|
by: liz0001 |
last post by:
Hi,
I need to access an .asp subroutine file from a subdomain.
i.e. the website is at www.domain.com and I want to access the same subroutines on the subdomain mobile.domain.com.
The...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
|
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,...
| |