0% found this document useful (0 votes)
4 views

9 Functions

The document outlines the fundamentals of functions in programming, including their structure, creation, and usage. It covers defining functions with arguments, setting default values, and the difference between call by value and call by reference. Additionally, it provides examples of function definitions and prompts for user input to demonstrate practical applications.

Uploaded by

KwongYew Ngu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

9 Functions

The document outlines the fundamentals of functions in programming, including their structure, creation, and usage. It covers defining functions with arguments, setting default values, and the difference between call by value and call by reference. Additionally, it provides examples of function definitions and prompts for user input to demonstrate practical applications.

Uploaded by

KwongYew Ngu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Functions

Session learning outcomes:

● Describe elements of a function


● Build a function to serve an objective

add all and divide by 3


(2(1 2 , 3) =
mean ,


argument

Swirl ( 3

* argument

Functions
does that behind the screen
● Are embedded codes
● Can be pre-written (in base or other R packages) or
created
● Consists of:
○ Name of function
○ Arguments
Function definition
● Create:

> Square_it ← function (input_variable1) {

result ← input_variable1^2

return (result) # can omit

● Use: > Square_it ( 10 )

Function definition
Argument
● Create:

> Square_it ← function (input_variable1) {


Name result ← input_variable1^2

return (result) # can omit

● Use: > Square_it ( 10 )


Function definition
Argument
● Create:

> Square_it ← function (x) {


Name result ← x^2

return (result) # can omit

● Use: > Square_it ( 10 )

Function definition
Argument
● Create:

> Square_it ← function (x) {


Name y ← x^2

return (y) # can omit

● Use: > Square_it ( 10 )


Function definition
Argument
● Create:

> papaya ← function (banana) {


Name y ← banana^2

return (y) # can omit

● Use: > papaya ( 10 )

example :

my-area < function (length width)


,

result e=
length * width
return (result)

Can we have more than one argument?


Can we have more than one argument?
> multiple_them ← function (p, q, r, s) {

answer ← p * q * r * s

return (answer)

> multiple_them (1, 56, 89, 2345)

Can we set default values for arguments?


Can we set default values for arguments?
> multiple_them ← function (p = 1, q = 2, r = 3 , s = 4) {

answer ← p * q * r * s

return (answer)

assigned
relea }

> multiple_them (1, 56, 89, 2345) ->


11687480

> multiple_them ( ) -> /x 2x3 + 4


=

24

values above them (1 , 56, 89)


if : assigned , multiple -
-> 1x5678944 :
19936

if : no assigned values ,
multiple-them() -> error

Call by value vs Call by reference


> add_one ← function (x) {

x ← x+1

print (paste (“x value in function is”, x))

return (x)

>x←2

> add_one (x)

> print (paste (“x value outside function is”, x))


⑲ -
value")
Call by value vs Call by reference
> add_one ← function (x) {

x[1] ← x[1] + 1

print (paste (“x value in function is”, x))

return (x)

>x←2

> add_one (x)

> print (paste (“x value outside function is”, x))


Let’s practice! In groups of three:
& Pseudocode :

create interactive
#

session

# Prompt user for


integer input between O to 9 ⑪

#Convert to colour
using if else statement ②

# Return Cobour ⑬

Session I = function (([


"Enter ")
- readline
* (prompt =
integer between 0 to 9

x = -
as. numeric (A)

if (X
-

0)[ ② or : Colours - c ("black" "brown"


,
,
"
red"
,
Colour e- "black

& else if (X==11[ "Orange" "yellow" "green"


,
,
,

Colour e- "brown" "blue" "Molet"


, "gray" ,

& else if (x = =
2)[
"White")
slour" red"
3 else if (x = =
3) [ Colours -Colours [x+17
Glou e- "orange" *
& else if (X =
=
4) [ [ 8
* -

Cobur =- "yellow" Colour-colours [x+1)


& else if (X =
=
5) [
Gobur=-"green" color="gray"
3 else if (X =
=
61[
cobur- "blue"

& else if (X = = 7= E
other method :
Cobur - "violet"

& else if (X =
=
8) [ or : use a lookup table
Cobur -

"gray"
numbers <- 0 9
&
:

else if (X =
=
9) [
names (numbers) < C(
-
"black",
cobur=- "white"
...

3
cat ("The colour selected is "colour)
3
Test 2 2020 (Past Years Question(

sesion 1 < function ( ([

#Prompt user for


input
WI-readline (prompt =
"Enter width in m")
I -readline (prompt ="Enter length in m")

#convert
string input to numeric value

w= -
as numeric (w)
-> al
1 < =
as numeric (1) 0 2
. 1 i2 3
,
.
, ,

calculate area
Integer 1, 2 , 3 ,
4 , I
area E-W # 1

asses
#

are q

if(area = 5 & area =7) <


cat C"Section area is", area)
if ( 3
- * IX
3 else[ . . .

Session I
Belsed
3
.
Y Y Y_Y
I
. .
. . . .

3
2
Area =
Mr

Area < function ()[


prompt
& etradline (h"enter radius of arch")
re- as numeric Cr)

ae- bit Un 2

if (a > 10 & 9220) [


"
cat C"area is
,
9)

3 else if (a) 30) [


cat( "now" , 9)
3 else if (a < 100) [

cat C"woh" 9)
,

3
Functions
Session learning outcomes:

● Describe elements of a function


● Build a function to serve an objective

Integer -> 1 2 3 4 5
, , , ,
** K
numeric -> 1 ,
2 1 3 1 , 4 5 2
,
, .
,
,

You might also like