473,395 Members | 1,532 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.

C++ Subroutines

compman9902
105 100+
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.
May 3 '07 #1
5 3572
JosAH
11,448 Expert 8TB
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
May 3 '07 #2
compman9902
105 100+
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
May 4 '07 #3
svlsr2000
181 Expert 100+
Though its a bad practice You can use Macros. but u have to make sure you dont use the macro any where else.
May 4 '07 #4
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.
May 4 '07 #5
compman9902
105 100+
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
May 4 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
7
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
3
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...
5
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...
1
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
1
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. ...
0
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. ...
0
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. ...
10
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
marktang
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,...
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.