Avoid warning

New to FreeBASIC? Post your questions here.
Post Reply
Topito216
Posts: 18
Joined: Dec 11, 2009 13:13
Location: Spain

Avoid warning

Post by Topito216 »

Hello.
Firts of all thanks to people who built the fbmath library , I think is wonderfull.

I have made the following program in order to solve matrix operations.
SUMA,RESTA,MULTIPLICACION=Addition, Subtraction, Multiplication

It works ok but I get 3 blue warnings that I dont know how to avoid. I can solve it writing the word return 666 for example but the really return result is a matrix not an only number.

this is the code:

Code: Select all

#INCLUDE "fbmath.bi"


Function SUMA(MA() As Double,MB() As double) As Integer
	If UBound (MA,1)<>UBound (MB,1) Then Print "NO SE PUEDEN SUMAR, LA MATRIZ ES A": Exit Function
	If UBound (MA,2)<>UBound (MB,2) Then Print "NO SE PUEDEN SUMAR, LA MATRIZ ES A": Exit Function
	Dim As Integer I,J
	For I=1 To UBound (MA,1)
		For J=1 To UBound (MA,2)
			 MA(I,J)=MA(I,J)+MB(I,J)
		Next
	Next
End Function
Function RESTA(MA() As Double,MB() As Double) As Integer
	If UBound (MA,1)<>UBound (MB,1) Then Print "NO SE PUEDEN RESTAR, LA MATRIZ ES A": Exit Function
	If UBound (MA,2)<>UBound (MB,2) Then Print "NO SE PUEDEN RESTAR, LA MATRIZ ES A": Exit Function
	Dim As Integer I,J
	For I=1 To UBound (MA,1)
		For J=1 To UBound (MA,2)
			 MA(I,J)=MA(I,J)-MB(I,J)
		Next
	Next
End Function
Sub PINTAMATRIZ(MA() As Double)
	Dim As Integer I,J
	For I=1 To UBound (MA,1)
		For J=1 To UBound (MA,2)
			Print MA(I,J),
		Next
		Print ""
	Next
End Sub
Function MULTIPLICA(MA() As Double,MB() As Double,MC()As Double) As Integer
Dim As Integer I,J,K,N,M,P
	
	For I=1 To UBound(MA,1)
		For J=1 To UBound(MB,2)
			MC(I,J)=0
			For K=1 To UBound(MA,2)
				 MC(I,J)=MC(I,J)+MA(I,K)*MB(K,J)
			Next K
		Next J
	Next I
End Function


DIM AS DOUBLE  Det                   ' Determinant
DIM AS INTEGER ErrCode               ' Error code
Dim As Double A(1 To 3,1 To 2)= {{1,2},{2,2},{6,9}}
Dim As Double B(1 To 2,1 To 3)= {{1,1,8},{1,1,7}}
Dim As Double C(1 To 3,1 To 3)
Dim As Double D(1 To 3,1 To 3)= {{1,1,8},{1,1,7},{1,3,3}}
Print "MATRIZ A"
PINTAMATRIZ (A())
Print ""

Print "MATRIZ B"
PINTAMATRIZ (B())
Print ""


Print "MULTIPLICA"
Print ""
MULTIPLICA (A(),B(),C())
PINTAMATRIZ (C())



SUMA (A(),B())
Print "SUMA"
PINTAMATRIZ (A())
Print ""
RESTA (A(),B())
Print "RESTA"
PINTAMATRIZ (A())
Print ""

Sleep
Cls

PINTAMATRIZ (D())
Print ""
GaussJordan D(), Det
SELECT CASE MathErr
  CASE MatOk
    PINTAMATRIZ (D())
    PRINT : PRINT "Determinant =", Det
    Print ""
    GaussJordan D(), Det
    IF MathErr = MatOk THEN
      PINTAMATRIZ (D())
    END IF
  CASE MatErrDim
    PRINT "Non-compatible dimensions!"
  CASE MatSing
    PRINT "Quasi-singular matrix!"
END Select
Sleep
And this is the 3 blue warnings:
Make done
C:\Program Files (x86)\FreeBASIC\fbc -s console "SUBRUTINAS_MATRIZ.bas"
SUBRUTINAS_MATRIZ.bas(13) warning 14(0): Function result was not explicitly set
SUBRUTINAS_MATRIZ.bas(23) warning 14(0): Function result was not explicitly set
SUBRUTINAS_MATRIZ.bas(44) warning 14(0): Function result was not explicitly set


Thanks very much.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Post by dkl »

Well, if a function's result isn't needed for anything, then it's best to use a sub, which is similar to a function except that is has no return value.
Topito216
Posts: 18
Joined: Dec 11, 2009 13:13
Location: Spain

Post by Topito216 »

dkl wrote:Well, if a function's result isn't needed for anything, then it's best to use a sub, which is similar to a function except that is has no return value.
Yes, I know that, and it's the reason that for to print a matrix I call a subroutine but I need a function because the return es an array.

Best regards
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Post by TJF »

Topito216 wrote:Yes, I know that, and it's the reason that for to print a matrix I call a subroutine but I need a function because the return es an array.

Best regards
Your result is in the MA() array. This array is passed bidirectional (read/write) in the parameter list. In this code you do not need an return value. Just remove FUNCTION by SUB as dkl mentioned and it will be OK.

It may be different if you like to return an error status. Then you should use a function and return ie zero for OK, 1 for dimension missmatch, 2 for ... and your calling code can output an error text regarding the function value.
Topito216
Posts: 18
Joined: Dec 11, 2009 13:13
Location: Spain

Post by Topito216 »

TJF wrote:
Topito216 wrote:Yes, I know that, and it's the reason that for to print a matrix I call a subroutine but I need a function because the return es an array.

Best regards
Your result is in the MA() array. This array is passed bidirectional (read/write) in the parameter list. In this code you do not need an return value. Just remove FUNCTION by SUB as dkl mentioned and it will be OK.

It may be different if you like to return an error status. Then you should use a function and return ie zero for OK, 1 for dimension missmatch, 2 for ... and your calling code can output an error text regarding the function value.
Thank you very much. I'll change function by sub. I did not know that's it's not needed use function because it's a bidireccional.

I apreciate very much your help.
I have to do a PhD job and I am studing freebasic and Visual basic.net pararellment and every day I am convinced I'll use freebasic.
Bye
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Post by TJF »

You're wellcome!

BTW:

You can download a ready to use Linear Algebra library here (including addition, subtraction, multiplication, division and other operators).
Post Reply