4 views

Uploaded by Juan

UCLA Pic 10A Slides

- modu
- FH_Ident-Profil_76_en-US (1) (1).pdf
- Macros
- Modular Programming in QBASIC
- Hints Pl
- OOALV_NOTES_WITH_PROGRAMS(1)santosh.pdf
- Project Res_Sim_Assignment SP17 1
- The Language of Technical Computing
- cvf_avis
- Week1 Procedural Decomposition
- Modulization Technique
- Excel VBA Basics
- Introduction to NATURAL.pdf
- macro-sas
- Create View With Parameter
- RR3036.DLL User's Manual V2.6
- Mock Exam 2010 S2
- Random Number Generator Using 8051
- Montana 2002 - Strongly Typed Genetic Programming
- f90intro

You are on page 1of 22

, parametern) {

// code, pretty much independent of everything

else.

return return_type_variable;

}

Parts of a function

1.

also be void, which does not return a value (more on this

later). There can only be ONE return type.

2.

names. Make sure the function name is not a reserved

word, and it should be descriptive.

3.

data types, excluding void. If using void, it must be the

only parameter.

4.

Only a single variable can be output.

Function Declarations

Recall

you need to declare it before main.

If

functions, they should be listed in reverse order in which

theyre called.

This

this is to declare your functions right after the namespace

line.

Function Declarations

Or

Function Declarations

In

declaration, it could

has

has a variable name specified for each parameter inside

parentheses

has a variable name different to the one used in the

definition of functions

Function Name

The

If

suggest following the convention that the first word is

lowercase and all subsequent words are uppercase.

You

becoming less common.

Function overload

In

their parameters are different; either because they have a

different number of parameters, or because any of their

parameters are of a different type.

Function overload

Another

example

Function overload

Remember

cin.ignore() ?

cin.ignore(void)

cin.ignore(int)

cin.ignore(int,

char)

Function overload

Note:

alone!

Modularization

A

other words, aside from the input parameters, a

function does not generally know or have access to

the variables defined in main and other functions.

This

to worry about using the name variable name

multiple times.

There

but we will see that good style is to make things are

compartmentalized as possible.

Separation of variables

In

general, the x in

cube is just a dummy

variable, which has

nothing to do with the

variable names in

main or in any other

function.

This

is part of the

reason why we

declare cube above

without a variable

name, just the data

types. It is because

the variable names

are immaterial, and

double cube(double);

int main() {

// These variable names in main dont care

about cube

double x=2.0;

cout<<cube(x);

}

/** Finds the cube

@param x is the number

@returns the cube of x, i.e., x*x*x or x^3

*/

double cube(double x) {

// The variable x is completely independent of

main

return x*x*x;

}

Separation of variables

Even

though I

changed the dummy

variable from x to y,

this is still the exact

same program.

double cube(double);

int main() {

// These variable names in main dont care

about cube

double x=2.0;

cout<<cube(x);

}

/** Finds the cube

@param x is the number

@returns the cube of x, i.e., x*x*x or x^3

*/

double cube(double y) {

// The variable x is completely independent of

main

return y*y*y;

}

Side Effects

It

no trace of its existence except for returning a value.

The

Things

This

Procedure

There

just run a set of code and

dont return a value. For

example, suppose I print a

welcome message

customized for a name

that does NOT return a

value. It can have as many

parameters as desired, but

its return type is void, and

rather than returning a

variable with a value we

simply write return;n f

the

return; statement is

void Welcome(string);

int main { }

/** */

void Welcome(string name) {

cout<<Welcome

<<name<<!\n;

return;

}

Predicate

A

bool validInput(int n);

bool isThatYourFinalAnswer(string answer);

Predicates

transform a statement like

if(sqrt(x*x+y*y)<=radius))

into

if(insideCircle(x,y))

Another

reason to

include the return; in a

function is to terminate

it early.

if n=10.

@param n is the input number

*/

void printStatements(int n) {

Also

cout<<Well hi there\n;

if(n==10) {

cout<<You are special\n;

return;

}

cout<<This is not going to print if 10 is

input\n;

cout<<I wish I was special :(;

cout<<So very special ... \n;

return;

}

note the

comments do not have

@return because there

is no value returned.

All

/** */

int sign(double x) {

if(x>0) return 1;

if(x<0) return -1;

// Error. The case x == 0 is not

covered.

}

All

/** */

int sign(double x) {

if(x>0) return 1;

if(x<0) return -1;

// The case x == 0 is now covered.

return 0;

}

Example: Factorization

We

its prime factors, sorted in order from smallest to biggest.

Notice we have to count repetitions.

15 = 3*5

24 = 2*2*2*3

It

divisor (>1) of a number.

Example: Factorization

This

Returns

divisor of a prime number is itself.

- moduUploaded bySidhish Kumar
- FH_Ident-Profil_76_en-US (1) (1).pdfUploaded byeman sulaeman
- MacrosUploaded byqthermal
- Modular Programming in QBASICUploaded bynilshankar12
- Hints PlUploaded byfelipe_bool
- OOALV_NOTES_WITH_PROGRAMS(1)santosh.pdfUploaded bySriram Suresh M
- Project Res_Sim_Assignment SP17 1Uploaded byPriozky Pratama Purba
- The Language of Technical ComputingUploaded byyogesh sharma
- cvf_avisUploaded byDaniel Jacks
- Week1 Procedural DecompositionUploaded byDren Hoti
- Modulization TechniqueUploaded byritesh
- Excel VBA BasicsUploaded byKunal Bhodia
- Introduction to NATURAL.pdfUploaded byJorge Armas Ramirez
- macro-sasUploaded byppaulyni33
- Create View With ParameterUploaded byAlex Enrique Valencia
- RR3036.DLL User's Manual V2.6Uploaded byEduard Elrick
- Mock Exam 2010 S2Uploaded byTimothy Fata Meafou
- Random Number Generator Using 8051Uploaded byUmar Nawaz
- Montana 2002 - Strongly Typed Genetic ProgrammingUploaded byegonfish
- f90introUploaded byElaine Camilo
- ECE4436 Reloc AL Coding Standard 9S12Uploaded bynguyentran263
- F2003 Part 1, Enhaced Data Type Facilities(Allocatable Components, OOP) 1999 Included in 1539-1Uploaded byitetejac
- Bug Classification Using Program Slicing MetricsUploaded byGabriel Rolim
- Lesson Two TextUploaded byRelu Chiru
- Compiler 7Uploaded byAdarsh Srivastava
- Vl Argparse.mUploaded bydeeparahul2022
- ch05.pptxUploaded byRendi Watulingas
- WorkInstruct1.docUploaded byAbhay Yadav
- c Pgm AlgorithmUploaded byshankar
- Question Ans UVMUploaded byDan Kumar Yadav

- all-models.pdfUploaded byMayur Patil
- Machine Learning an introductionUploaded byAnoop Cadlord
- Induction ContradictionUploaded bysimpleid
- ml2000Uploaded byAbdul Dakkak
- lec11Uploaded byShengFeng
- Lower BoundsUploaded byFajar Haifani
- Chapter-3 (1)Uploaded byEllen
- Character ClassUploaded by9897856218
- discrete math notes 17-05-16.pdfUploaded byAnonymous 0YJ7B5MK8G
- A DeductiveUploaded byRaguman Ragu
- AlexUploaded bysudha24_7
- Truth Table1.pdfUploaded bySanjay Nath
- Modus PonensUploaded bySudarshan Bellary
- If and only ifUploaded byPatrick Mugo
- There is No Standard Model of ZFC2Uploaded byJaykov Foukzon
- Published by Mouton Gruyter-Theoretical Linguistics. 1-Mouton Gruyter (1974)Uploaded byBelinda Angel
- optimization Lecture 2Uploaded byKatie Cook
- Information TheoriesUploaded byMaria Karmela Louise Vinco
- The Godelian Puzzle BookUploaded byJulia
- Answers to Exercises for Section 2.2.docUploaded byjriver64
- NPDA problem solution manualUploaded byactayechanthu
- Durga Core Java BookUploaded bySomesh Suman
- Pattern_Matching_Algorithms.pdfUploaded byMarcus Braga
- Regression in Modal LogicUploaded byIvan José Varzinczak
- Bibliography IndexUploaded bygodwsky
- FormalizProp Tarea de Mate-logicaUploaded byJV vargas
- lesson-4Uploaded byVienna Corrine Q. Abucejo
- Francesco Mazzoli - Bertus: Implementing Observational EqualityUploaded byAndrás Kovács
- finite automataUploaded bySujala V Koparde
- Erisen, Dogan - Wittgenstein. Logical Atomism and the True Nature of Logic.pdfUploaded byMicaela Bonacossa