1 / 14

Le funzioni

Le funzioni. UD. 8 – L’organizzazione dei programmi p. 309 LS Tron 4TC 06/07. Che cos’è una FUNZIONE. E’ un modulo SW interno al programma Ha una struttura simile alle procedure: Riceve dal main i valori assegnati ai parametri formali MA si comporta diversamente: RESTITUISCE UN VALORE.

kelli
Download Presentation

Le funzioni

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lefunzioni UD. 8 – L’organizzazione dei programmi p. 309 LS Tron 4TC 06/07

  2. Che cos’è una FUNZIONE • E’ un modulo SW interno al programma • Ha una struttura simile alle procedure: Riceve dal main i valori assegnati ai parametri formali • MA si comporta diversamente:RESTITUISCE UN VALORE

  3. Come si dichiara ? • Function funz(parametri:tipo):TIPO; Begin …… End; Come si nota, SI DEVE INDICARE OBBLIGATORIAMENTE QUAL E’ IL TIPO (integer, real, boolean…) DEL VALORE CHE VIENE RESTITUITO AL MAIN

  4. Come restituisce il valore ? • Function funz(parametri:tipo):TIPO; Begin ……funz:= … …… End; All’interno della funzione COMPARIRA’ OBBLIGATORIAMENTE un’istruzione di assegnazione contenente a sinistra il NOME DELLA FUNCTION

  5. Esempio • Es.Function Somma(A,B:INTEGER):integer; begin Somma:=a+b; end;

  6. Il ritorno (nel main) • Le funzioni vengono usate nel Main COME SE FOSSERO Variabili o espressioni: • Nell’es. di prima si potrebbe pensare a: ... Totale:=somma(x,y)*2; … Writeln(somma(3,5):10);… IF somma(C,D)>100 THEN…

  7. Funzioni predefinite (built in) • Es. Abs(x)restituisce il valore assoluto • Se non esistesse, il programmatore potrebbe crearla

  8. Funzioni es. 1 Program ValoreAssoluto; VAR y:Real; Function Abs(x:real):real; BEGIN if x<0 then Abs:=-x else Abs:=x; END; Begin readln(y); writeln(Abs(y):10:2); End.

  9. Funzioni es. 2 (al posto di Odd(x)) Program PariDispari; VAR y:Integer; Function Pari(x:integer):BOOLEAN; BEGIN if x MOD 2 =0 then pari:=TRUE else pari:=FALSE; END; Begin readln(y); IF pari(y) = true (oppure IF pari(y)) then writeln(‘Pari’) else writeln(‘Dispari’); End.

  10. Funzioni Built in di Gestione stringhe in Pascal (Ringrazio il Prof. Catena per il file da cui sono stati presi gli esempi) Si suppone che: var S,S1:string; { S:=’INFORMATICA’;} N,K:integer; Operazione Function Length(S:string):integer Descrizione Funzione: restituisce la lunghezza della stringa (numero di caratteri) Esempio di uso N:=Length(S); {11} O writeln(Length(S));

  11. Si suppone che: var S,S1:string; { S:=’INFORMATICA’;} N,K:integer; Operazione Function Copy(S:string;N,K:integer): string Descrizione Funzione: restituisce da S la sottostringa formata da K caratteri a partire dal carattere di posto N Esempio di uso S1:= copy(S,3,5); {FORMA} o writeln(copy(S,3,5));

  12. Si suppone che: var S,S1:string; { S:=’INFORMATICA’;} N,K:integer; Operazione Function UpperCase(S:string): string Descrizione Funzione: restistuisce la stringa S in caratteri maiuscoli Esempio di uso S:= UpperCase(S); o writeln(UpperCase(S)); Operazione Function UpCase(CH:char): char Descrizione Funzione: restituisce il carattere CH in maiuscolo Esempio di uso CH:= UpCase(CH); {se CH=’a’ , CH=’A’} o writeln(UpCase(CH));

  13. Operazione Function Ord(CH:char): integer Descrizione Funzione: restituisce la posizione del carattere nel codice ASCII Esempio di uso N:= Ord(CH); {se CH=’A’ , N=65} o writeln(Ord(CH)); Operazione Function Chr(N:integer): char Descrizione Funzione: restituisce il carattere che occupa la posizione N nel codice ASCII Esempio di uso CH:= Chr(N); {se N=65, CH=’A’ } o writeln(Chr(N));

  14. Operazione Function Succ(CH:char): char Descrizione Funzione: restituisce il carattere successivo nel codice ASCII Esempio di uso CH:= Succ(CH); {se CH=’A’ , CH=’B’} o writeln(Succ(CH)); Operazione Function Pred(CH:char): char Descrizione Funzione: restituisce il carattere precedente nel codice ASCII Esempio di uso CH:= Pred(CH); {se CH=’A’ , CH=’@’} o writeln(Pred(CH));

More Related