Week 10 - Function
Week 10 - Function
Function in Python
What is Function?
Outline Practice I
Arguments
Practice II
Return
Practice III
Practice IV
Submission
Function
What is Function is a block of code that
will be run if it is called.
Function? A function is a block of organized, reusable
code that is used to perform a single, related
action. Functions provide better modularity
for your application and a high degree of
code reusing.
How to create a function?
Code
Output
Can I change the function name? Sure! Change your function name as you wish
Don’t forget, after you named the function, you have to add parentheses ()
Practice I
For student whose last For student whose last
digit of student id is digit of student id is even
odd
Recreate previous program, create a function Recreate previous program, create a function name
name helloMe, and the output will be Hello, helloYou, and the output will be Hello, your student ID
your name
Arguments
What is arguments?
Arguments mean we are giving information into the function.
Output
Example of function with 2 arguments
Code Output
Practice II:
Recreate the greeting() function with 2 argument (name
and id)
Expected result :
Return
What is return?
The return keyword in a function tells the function to pass data back. When we call the
function, we can store the returned value in a variable. Return values allow our
functions to be more flexible and powerful, so they can be reused and called multiple
times.
Example of return
Code Output
Practice III:
For student whose last For student whose last
digit of student id is digit of student id is even
odd
Recreate the previous program, create a
Recreate the previous program, create a
function, the name is up to you. The function
function, the name is up to you. The function
will calculate the area of a Parallelogram
will calculate the area of a Triangle using 2
(jajar genjang) using 2 arguments (base,
arguments (base, height). You have to use
height). You have to use the return to obtain
the return to obtain the result.
the result.
Practice IV:
Please create a simple program to determine grade predicate based on the score of students:
A : > 85
B : 70 - 85
C : 60 - 69
D : <60
Then use the data to return the result to the user (student's name and their grade).
Input:
Name : Kartika
Avg Grade : 80
Output:
Student : Kartika
Pass the class with B
Step to solve:
Function:
Create a function to determine the grade based on the average grade as the parameter.
Create if for all the possible scenarios based on the question.
The grade will be returned using the return keyword
Create a function to print the result with the grade and student name as the parameter.
Grade A and B pass the class
Grade C and D need retake
Main:
Create input for name and the average grade and save it in variable
Call the function to determine the grade with the average score and save the result in a variable
Call the function to print the result with the student name and the grade from the previous function
as the parameter.
Expected Result:
Additional Practice:
(Optional)
Please create a function to calculate the sum of number from 0 - the number in the
argument
Example:
sum_number(10) = 55 sum_number(5) = 15
0+1+2+3+4+5+6+7+8+9+10= 55 0+1+2+3+4+5 = 15
Practice I and III Screenshot your program code and output depending on your student ID last digit