Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER PROGRAMMING SKILLS

Similar presentations


Presentation on theme: "COMPUTER PROGRAMMING SKILLS"— Presentation transcript:

1 COMPUTER PROGRAMMING SKILLS 1439-1440
CHAPTER 7: Functions COMPUTER PROGRAMMING SKILLS

2 Outline Functions Built-in Functions User-defined Functions
input() Outline Functions Built-in Functions User-defined Functions Create and Call a Function Parameters Return Values Exercises

3 input() Functions A function is an organized block of statements that performs a task, and it can be reused over and over. Functions are reusable codes of programs. Function may be built-in functions or user-defined functions Built-in functions are a part of Python packages and libraries. Python provides set of built-in functions like print(), input (), etc.… User-defined functions are created by developers to achieve some specific tasks. Function name includes parentheses and it could has parameters.

4 Built-in Functions The following list presents a set of most common built-in functions of Python. An iterable is an object that you can get an iterator from. Function Description abs() Returns the absolute value of a number max() Returns the largest item in an iterable bin() Returns the binary version of a number range() Returns a sequence of numbers, starting from 0 and increments by 1 (by default) chr() Returns a character from the specified Unicode code. min() Returns the smallest item in an iterable int() Returns an integer number next() Returns the next item in an iterable hex() Converts a number into a hexadecimal value sum() Sums the items of an iterator input() Allowing user input open() Opens a file and returns a file object pow() Returns the value of x to the power of y round() Rounds a numbers print() Prints to the standard output device str() Returns a string object

5 Built-in Functions # Program 1, use built-in functions number = 5
# the power of print("Power=",pow(number, number)) # the binary value of number print('The binary equivalent is:', bin(number)) # the character of input value print("The char equivalent = ", chr(97)) # the absolute value num = print('Absolute value of is:', abs(num)) Power= 3125 The binary equivalent is: 0b101 The char equivalent = a Absolute value of is: 30.33

6 User-defined Functions
Developers can create their own functions, that are used anywhere in program and any number of times. To define a function in Python, you should follow some rules: Function is defined by def keyword, followed by the its name, and parentheses ( ). Parentheses may enclose some input parameters. Next, it is ended by colon : . Block of statements are written with indented. The statement return exits the function. To call a function, use its name followed by parenthesis # a syntax of user-defined function def functionName( parameters ): #the function block statements return

7 Create and Call a Function
Following, we create a simple function to print messages every time we call it. # Program 2, create a simple function def sayHello(): # The block of the function print("Hello in functions chapter") print("Here, we will define functions") print("Enjoy") return # End of function sayHello() # call the function Hello in functions chapter Here, we will define functions Enjoy

8 Create and Call a Function
The name of the function is sayHello . This function takes no parameters. There are no variables declared in the parentheses. The function has four statements. Three statements to print a string, and return statement to end the function execution. This function didn’t return results. The function is called twice by using its name followed by parentheses sayHello(). The function can be executed many time without rewriting the same code again.

9 Parameters Usually, functions need some information and values to fined the results. These parameters are like variables. They are values that pass to the functions. Parameters are given by parentheses in the function definition, and separated by commas. The special variables in functions are parameters. Whereas, values that passed to the function are arguments. Parameters have a positional case and they must be followed by the same order in the definition. When the function is called, values are assigned to the function.

10 Parameters Following, we have a function to find the maximum between two valus. # Program 3, a function with parameters def find_max(v1, v2): if v1 > v2: print('The maximum is:', v1 ) elif v1 == v2: print(v1, 'is equal to', v2) else: print('The maximum is:', v2) find_max(5, 9) # pass values directly as arguments a = 4 b = 7 find_max(a, b) # pass variables as arguments The maximum is: 9 The maximum is: 7

11 Parameters In program 3, the function called find_max is defined to find the maximum value between two passed values. The function find_max uses two parameters called v1 and v2. In the first call statement, the function is called and values are given directly as arguments. Based on the position, the first value passed to the first parameter, while the second value passed to the second parameter. In the second call statement, the function is called with variables as arguments . The values of arguments a and b are assigned to parameters v1 and v2 respectively. 9 5 v2 v1 9 4 b a 5 v2 v1

12 Return Values The parameter value is passed into a function. In other side, the function can produce a value. We use return statement to return values from a function. Return statement end the function’s execution and can returns the results back to the caller. The add_Numbers function returns the sum of the two valus. The values are passed to the function. It find the result and then returns that result. Note: A return statement without a value is equivalent to return None (nothingness). # Program 4, return results def add_Numbers(x, y): z = x + y return z # return the result to call statement sum1= add_Numbers(2, 3) print("The returned value is" , sum1) The returned value is 5

13 Exercises 1: True or False
In python, function can be used one tome only in each execution. T F In Python, all functions may are user-defined functions T F The function next() is an example of user-defined functions T F Function is defined by def keyword. T F The detention of functions consist of def keyword, function name, then colon : T F The statement return exits the function T F To call a function, use its name followed by parenthesis T F Arguments is passed to the parameters based on the position T F return statement returns values from a function to caller T F

14 Exercice 2: write a Python code
Using function, write a Python code to print the binary value, and the square value of input decimal number. Using self-define function, write a code to sort four input values. Using self-define function, write a code to find the factor of input value.


Download ppt "COMPUTER PROGRAMMING SKILLS"

Similar presentations


Ads by Google