Forumite Members › General Topics › Tech › Makers & Builders › Programming/Code tips › Learning to program
Tagged: Android, app, program, programming
- This topic has 133 replies, 6 voices, and was last updated 7 years, 3 months ago by
Wheels-Of-Fire.
-
AuthorPosts
-
October 14, 2018 at 4:49 pm #27171
I stand by what I said:
&Avariable passes the address of Avariable, and the action of a call-by-reference function is to pull out the value contained in that memory space and use it in the function. If the function then changes Avariable it also changes the value stored in the calling routine..
In your ‘by reference’ example the value stored in that address is changed by the action of the function, whereas the similar function does nothing in the call by value example.
This is harder to write than to do!
October 14, 2018 at 4:58 pm #27173In C++ you could say:
SomeType *ptr = someAddress;
SomeType &ref = *ptr;
assert(&ref == ptr);The assert will be true
October 14, 2018 at 5:05 pm #27175Well we both mean the same thing then ?
My problem was with the wording of their second question. I am just being pedantic but they insist that we take note of the literal meaning of their questions so we get the answers they want. When I do they mark me wrong so miffed ?
October 15, 2018 at 8:16 am #27194I’m missing something; as conflating your first and second examples there seems to be no ambiguity.
October 15, 2018 at 2:19 pm #27204Its in the question. The first one asks for a value on RETURN from a function. The second one asks for the value on CALLING a function, no mention is made of executing the function let alone returning from it. An earlier exercise asked us output the value of a pointer that we passed to a function straight after the function was called and before it returned so I answered question 2 as it was writtern after reading it twice. Just making sure im not being dumb before I raise the point on the edX forum ?
October 15, 2018 at 2:58 pm #27206I’m obviously a pedant! In the call by reference example if you set debug going and look at the variable values the value of Avariable in ‘Main’ will change as soon as you execute the first (and only) statement Avariable=50 in the function it never need return.
I’ll agree that there is ambiguity if you are just starting to learn the concept. The question is what I’d call an A* question if you get the correct answer as it demonstrates that a ‘return’ is not required. This could be important if this was in a real-time application that somehow changed (say) an interrupt.
btw What language are you learning, I do not recognise the syntax.
October 15, 2018 at 3:47 pm #27207I am extending my knowlage of C into C++. I must admit that I am a bit rusty with C so it is a good thing that the course covers the common basics before moving on to C++ specifics like classes.
I completed the introduction to C++ course in less than a week (with a 100% score I might add ?) and Im now part way through the intermediate course.
I don’t always use perfect syntax here but the code example I posted will compile and run in Visual Studio as I pasted it from there.
When I get in I will load up the code and step through it with Auto variable display turned on. I will see what the value of Avariable is when I just step into a function without executing any of its instructions.
October 15, 2018 at 4:32 pm #27211In case you were wondering “cout<<” is a newer alternative to “printf” using an output stream. Corse I may be teaching granny to suck eggs here ?
October 15, 2018 at 4:36 pm #27213Depending on your C++ compiler it may return before you can inspect the value!
You may need to add a do nothing loop such as incrementing a variable and outputting its value. Do this after the Avariable=50 line. Compilers can sometimes be too clever to do what you want!
[edit] actually cout has been around for a long while. Where I am happily willing to admit ignorance is in the subtleties of unicode strings. e.g link. Ascii and short strings are good enough for my purposes.
October 19, 2018 at 2:38 pm #27342Here is a thing i did not expect. The more I look into C functions the more I understand functions in Atari BASIC. For instance FRE(0) will return the amount of free user memory avaliable to BASIC and RND(0) will get you a random number. Both are functions and require an input variable. In this case it is 0 and is the same as null. I did not know why I had to send something before.
Another thing is C is strongly typed and so is Atari BASIC. You can have a simple variable that you must declare and assign at the same time with something like A=10. A will get 8 bytes in memory and it will be of type signed floating point. You can also have an array but it must be dimentioned first with something like DIM A(10). That will get you an array with ten 8 byte elements. Or you can have a matrix with DIM (10,10).
String variables are the last type you can have and they must be dimentioned too with DIM A$(10) or such like. That will get you 10 bytes of memory asigned as ATASCII (Atari ASCII) charachters. If you want to know the memory location of a string then you can use x=ADR(A$). This is handy for short machine code functions. A=USR(ADR(A$), argument, argument, etc). Put your routine in the string and BASIC will use it as a pointer. If you are going to return a value to A then there are 2 bytes in page 3 of memory to store your value.
October 19, 2018 at 3:44 pm #27343Oh yes the BASIC USR function will push the program counter on to the stack followed by your arguments followed by the number of arguments you passed. Your routine must pull the arguments and the number of arguements off the stack before returning with an RTS instruction or you will return to a nonsence address.
October 19, 2018 at 7:28 pm #27346Strong typing made it easier to write compact compilers. As memory/storage has grown languages and compilers have grown in size and complexity. Interpreted languages also needed to be compact so the drives were identical.
Finally at heart good programmers are lazy, and if you can make things easier with simple rules then that is what happened!
October 19, 2018 at 10:55 pm #27349Yes indeed I am lazy given the chance ? let someone else write a public domain function and I will use it ! That includes all the functions Microsoft makes avaliable. Include Windows.h at the start of your code and you can call most of them. I will put up a very simple bit of C that displays a “Hello world” dialogue box when I come off the beer tomorrow (It is friday night ?) A real windows box from a console app.
October 24, 2018 at 12:33 pm #27501As promised here is the tiny bit of C++ code that pops up a Windows dialogue box from a console App.
#include “pch.h”
#include <iostream>
#include <Windows.h>
int main()
{
int msgboxID = MessageBoxA(NULL, “Hello World”, “My Box”, 0);
return msgboxID;
}
If you paste this into Visual Studio it will compile and run as long as you have a fairly recent version of the Windows SDK installed.
October 24, 2018 at 12:47 pm #27502Why is there no easy way to rename a project from within Visual Studio ? I just noticed I named a small experiment “FeaderFiles” instead of “HeaderFiles”. Not really a problem this time but annoying.
October 24, 2018 at 2:35 pm #27503Why is there no easy way to rename a project from within Visual Studio ? I just noticed I named a small experiment “FeaderFiles” instead of “HeaderFiles”. Not really a problem this time but annoying.
Probably a Windows ‘History’ quirk. As most of my code is exploratory in nature (i.e. I really do not fully know what I’m doing), I will quite frequently branch into a direction then weeks later realise that it was a blind alley and have to jump back some steps. That is when historising helps, and I guess needs a ‘constant’ file name to do so.
October 25, 2018 at 12:50 pm #27523Quite likely, and I have now also read that it would be difficult to do consistently on larger projects, especially those involving collaboration.
October 25, 2018 at 1:32 pm #27524Is higgledy piggledy the new method of teaching or is it just a Microsoft thing ? I first noticed it in the Microsoft press Windows Internals books. These books would keep doing things like this.
We will now cover “something”. In order to understand “something” you must first understand “something else” that we will cover in a later chapter.
They then go on as if you already know what “something else” is. You end up having to read through the whole book four or five times, picking up what you can each time.
Now I am finding the same thing on the Microsoft C++ course on edX. At the end of the introduction course, past the final self test, they tacked on a video about C++ classes. The video said don’t worry if you don’t get everything going on here because it will all be explained in the intermediate course. The intermediate course starts off with some things about headers in Visual Studio I should have been told in the intermediate course and then moves on with “As we learnt about classes in part one” !
October 25, 2018 at 4:32 pm #27535I ment should have been told in the introduction course.
October 25, 2018 at 8:28 pm #27542I cannot comment as it is many moons since either myself or sons learnt C. At that time we were overseas where text books were much cheaper than here, and we loaded up on books written by an Indian gentleman (I cannot remember his name unfortunately). I do not remember much circularity in the learning curve. That however was in the dark ages before IDEs and ‘visual’ programming.
In these days I think I would need to spend time getting to grips with the IDE and gui design using a bare modicum of C before getting into the more difficult aspects of C Perhaps this may be the reason for your complaint.
Other things such as the relationship between pointers and arrays need you to be familiar with both, before ‘jumping back”. Ditto function pointers etc., or horror of horrors — self modifying code!
-
AuthorPosts
- You must be logged in to reply to this topic.
