Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Fundamentals of Programming – ITM200 Functions Zeinab Noorian © Copyright Youcef Derbal, 2022-2023 Agenda ▪ Functions ▪ Argument Passing ▪ Type Error ▪ Using Library Functions © Copyright Youcef Derbal, 2022-2023 F...

Fundamentals of Programming – ITM200 Functions Zeinab Noorian © Copyright Youcef Derbal, 2022-2023 Agenda ▪ Functions ▪ Argument Passing ▪ Type Error ▪ Using Library Functions © Copyright Youcef Derbal, 2022-2023 Functions Improve program clarity and maintainability by encapsulating instructions that implement a cohesive functional behavior into programming components called functions Functions are defined only once but can be invoked/used multiple times at will Similar to Python built-in functions such as input(), int(), float(), and print(). © Copyright Youcef Derbal, 2022-2023 Example Consider the problem of computing the area of the following geometric shape, which consists of two rectangles placed side by side: © Copyright Youcef Derbal, 2022-2023 Example © Copyright Youcef Derbal, 2022-2023 Structured Programming Define a function called area, and task it with the responsibility of computing the area of a rectangle Invoke/call the function using its name area and a list of arguments: length and width. © Copyright Youcef Derbal, 2022-2023 Function Definition def function-name( parameter-list) code-block return-statement function-name : area parameter-list : length_ , width_ return-statement : return ar © Copyright Youcef Derbal, 2022-2023 Function Call Returned-value-holder = function-name(argument-list) function-name : area argument-list : lnA , wdA returned-value-holder : area_A © Copyright Youcef Derbal, 2022-2023 Function Call The function call passes the values of lnA and wdA to length_ and width_ respectively. Once the function completes its execution it returns the value of ar , which is subsequently assigned to area_A , area_B © Copyright Youcef Derbal, 2022-2023 How functions work? Function Call Functions must be defined before they are called. This can be achieved by placing function definitions right at the top of the program file. The call arguments lnA and wdA are paired, in accordance to their order in the list, with the function’s parameters length_ and width_ respectively. Call arguments must have data types that match those of the respective function parameters. Ex: Since length_ is a float, lnA is expected to be a float or an int The returned value has the data type assumed within the function definition. © Copyright Youcef Derbal, 2022-2023 Fruitful vs Void Functions The function area() was defined to return a value and is hence categorized as a fruitful function. However, it is possible to define so-called void functions that do not return any values. Furthermore, it is also possible to define functions that do not have any parameters. © Copyright Youcef Derbal, 2022-2023 Function with return statement © Copyright Youcef Derbal, 2022-2023 Example Void (non-fruitful) functions return None © Copyright Youcef Derbal, 2022-2023 In-class practices: Function © Copyright Youcef Derbal, 2022-2023 Argument Passing © Copyright Youcef Derbal, 2022-2023 Argument Passing Although the value of n was changed inside the function increment(), this did not have any effect on the value of the variable count that was passed as argument to the function This is because n is a distinct variable, which has merely been assigned the value of the count variable at the time of the function call. The argument count was passed by value © Copyright Youcef Derbal, 2022-2023 Argument Passing The function parameter count and the global variable count are two different variables with different scopes. Changes made to the function parameter do not affect the global variable. Using count instead of n as the function parameter would yield the same program behavior © Copyright Youcef Derbal, 2022-2023 Argument Passing © Copyright Youcef Derbal, 2022-2023 Function - Local Variables Variables that are defined inside a function, such as n for the function increment(), are local variables to the function. Hence the rest of the program (outside the function) does not know of their existence. © Copyright Youcef Derbal, 2022-2023 Function - Local Variables Output : The x inside the do_count function and the x outside the function are two distinct variables. The one inside the function is local to the function and does not affect the value of the global x. Argument passing The list lst is modified because we are directly changing the first element of the list lst within the myFun function. In Python, lists are mutable, which means you can modify their elements in place. When you assign x = 20 inside the function, it modifies the original list lst because both x and lst refer to the same list object in memory. Argument passing The list lst remains unchanged because, within the myFun function, you reassign x to a completely new list [20, 30, 40]. This new assignment does not affect the original list lst because x becomes a reference to a different list object in the local scope of the function. The original list lst still exists outside the function and is not modified by the function. Argument passing Mutable data types like lists, dictionaries, and sets can be modified within a function even if they are defined outside the function. – This is because when you pass a mutable object to a function, you're actually passing a reference to that object, not a copy of the object. Therefore, changes made to the object within the function will reflect outside the function as well. Example Argument passing For variables with Immutable data types such as integers, floats, strings, and tuples, their values are directly accessible (can be read) within such function but cannot be modified. – When an immutable data type is passed to a function, and if there's an attempt to "change" its value inside the function, it results in creating a new object in the local function scope. The original object (defined outside the function) remains unchanged. © Copyright Youcef Derbal, 2022-2023 Example © Copyright Youcef Derbal, 2022-2023 Type Error Function calls made with arguments having types different from those expected for the function parameters lead to a type error. Output © Copyright Youcef Derbal, 2022-2023 TypeError A run-time error of category TypeError would also be generated if the returned value of a function call has a type that does not match the type expected in an expression that uses such function’s returned value. Output © Copyright Youcef Derbal, 2022-2023 Using Library Functions In addition to built-in functions such as int(), there are many other Python library functions that can be used. However, the invocation of these functions require the import of the modules that contain them. Ex: The functions factorial() and the randint() are two functions that compute the factorial and generate random integers, respectively. These require the following import statements: import math # contains factorial() import random # contains randint © Copyright Youcef Derbal, 2022-2023 Example Output randint(a,b): generates an integer n such that a

Use Quizgecko on...
Browser
Browser