# Classes programming

**URL:** <https://smallbasic.discourse.group/t/classes-programming/47>\
**Category:** General\
**Created:** [November 5, 2025, 4:07pm UTC](https://smallbasic.discourse.group/t/classes-programming/47 "2025-11-05T16:07:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JFD](https://yyz2.discourse-cdn.com/free1/user_avatar/smallbasic.discourse.group/jfd/32/27_2.png) [@JFD](https://smallbasic.discourse.group/u/JFD)\
**Post date:** [November 5, 2025, 4:07pm UTC](https://smallbasic.discourse.group/t/classes-programming/47/1 "2025-11-05T16:07:31Z")

</div>

**Hello everyone,**

I’ve been using SmallBASIC for a while now, and I’m facing a problem I can’t seem to solve.  
I create a lot of “classes”, which I find convenient even though it’s limited compared to other languages (I come from Pascal).

I’ve made a very simple example to explain what I mean.

 ![Screenshot_20251105_165705](https://global.discourse-cdn.com/free1/uploads/smallbasic1/original/1X/62f12cf7b5bbcf6c18d62fa49ecdb63ae448ee3f.png)

In this code, we have class1 (`c1`) and class2 (`c2`).  
If line 13 is commented out, it works and we get the expected result.

> external hello from class1  
> external hello from class2
> 
> Press any key to exit.

However, if you uncomment it, it _seems_ to work since the `out` method of class `c2` is indeed called from `c1`, but we still get an error!

> external hello from class1  
> internal hello from class2
> 
> - RTE-ERROR AT main.bas:4 \*  
> Description:  
> Invalid SUB pointer variable
> 
> Stack:  
> SUB: 22

I’m running on Linux (Kubuntu 24.04) and using the console version compiled from source.  
But I get the same result no matter which version of SmallBASIC I use.

Thanks — and sorry for the wall of text!!  
JF.

---

<div class="post-metadata">

**Author:** ![J7M](https://yyz2.discourse-cdn.com/free1/user_avatar/smallbasic.discourse.group/j7m/32/7_2.png) [@J7M](https://smallbasic.discourse.group/u/J7M)\
**Post date:** [November 6, 2025, 9:21am UTC](https://smallbasic.discourse.group/t/classes-programming/47/2 "2025-11-06T09:21:13Z")

</div>

Hi Jfd, SmallBASIC is strict with return values. If you use a function, the return value has to be used. For example by assigning it to a variable. In line 2 you are defining a function. That line 22 works, is somehow an unexpected behaviour. To get you program working, you can either use a subroutine or assign the return value.

With functions:

```auto
func class2()
  func m_out(msg)
    ?msg+" from class 2"
  end
  local vt
  vt.out=@m_out
  return vt
end

func class1()
  func m_out(msg)
    ?msg+" from class 1"
    'a = self.c2.out("internal hello")
    a = call(self.c2.out, "internal hello")
  end
  local vt
  vt.out=@m_out
  vt.c2=class2()
  return vt
end

c1=class1()
a = c1.out("external hello")
a = c1.c2.out("external hello")

```

With subroutine

```auto
func class2()
  sub m_out(msg)
    ?msg+" from class 2"
  end
  local vt
  vt.out=@m_out
  return vt
end

func class1()
  sub m_out(msg)
    ?msg+" from class 1"
    'self.c2.out("internal hello")
    call self.c2.out, "internal hello"
  end
  local vt
  vt.out=@m_out
  vt.c2=class2()
  return vt
end

c1=class1()
c1.out("external hello")
c1.c2.out("external hello")

```

I hope this solves your problem. Best regards, J7M

---

<div class="post-metadata">

**Author:** ![JFD](https://yyz2.discourse-cdn.com/free1/user_avatar/smallbasic.discourse.group/jfd/32/27_2.png) [@JFD](https://smallbasic.discourse.group/u/JFD)\
**Post date:** [November 6, 2025, 10:28am UTC](https://smallbasic.discourse.group/t/classes-programming/47/3 "2025-11-06T10:28:06Z")

</div>

Hi Joerg, thanks for the clarification.  
I really did overlook that.  
Bad Pascal habits…

It clearly says: “The definition of a function starts with the keyword FUNC and ends with the keyword END. It has a name name, can have parameters par1 to parN and must return a value value.”

> **[SmallBASIC | guide](https://smallbasic.github.io/pages/guide.html#SubroutinesAndFunctions)**
>
> SmallBASIC | One more basic

But I skimmed right over the “must”. RTFM
