COMPUTER PROGRAMMING SKILLS

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Structured programming
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Introduction to C Programming
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Munster Programming Training
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Topics Introduction to Functions Defining and Calling a Void Function
Introduction to Python
COMP 170 – Introduction to Object Oriented Programming
Variables and Expressions
Chapter 2 - Introduction to C Programming
Python: Control Structures
© 2010 David A Watt, University of Glasgow
Chapter 6 Functions.
Functions CIS 40 – Introduction to Programming in Python
Method.
Functions.
Chapter 2 - Introduction to C Programming
User-Defined Functions
Looping.
Python Primer 2: Functions and Control Flow
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Group Status Project Status.
Variables and Expressions
Chapter 2 - Introduction to C Programming
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6: User-Defined Functions I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
G. Pullaiah College of Engineering and Technology
CHAPTER 6: Control Flow Tools (for and while loops)
Chapter 2 - Introduction to C Programming
Loops and Simple Functions
Class code for pythonroom.com cchsp2cs
ITM 352 Functions.
def-ining a function A function as an execution control structure
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Functions John R. Woodward.
© Sangeeta M Chauhan, Gwalior
Presentation transcript:

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

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

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.

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

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 = -30.33 print('Absolute value of -30.33 is:', abs(num)) Power= 3125 The binary equivalent is: 0b101 The char equivalent = a Absolute value of -30.33 is: 30.33

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

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

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.

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.

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

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

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

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

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.