12th Computer Science Chapter 1 - PDF

Document Details

LivelyPoplar

Uploaded by LivelyPoplar

Silver Jubilee Matriculation Hr. Sec. School

Tags

computer science programming functions algorithms

Summary

This document is a chapter from a computer science textbook. It introduces the concept of functions in programming languages, explaining function specifications, parameters, and arguments.

Full Transcript

https://csknowledgeopener.com CHAPTER 1 Unit I FUNCTION 1.2 Functi...

https://csknowledgeopener.com CHAPTER 1 Unit I FUNCTION 1.2 Function with respect to Learning Objectives Programming language After the completion of this chapter, the A function is a unit of code that is student will be able to: often defined within a greater code structure. Specifically, a function contains a set of Understand Function Specification. code that works on many kinds of inputs, Parameters (and arguments). like variables, expressions and produces a Interface Vs Implementation. concrete output. Pure functions. 1.2.1 Function Specification Side - effects (impure functions). Let us consider the example a:= (24). a:= (24) has an expression in it but (24) 1.1 Introduction is not itself an expression. Rather, it is a The most important criteria in function definition. Definitions bind values writing and evaluating the algorithm is the to names, in this case the value 24 being time it takes to complete a task. The duration bound to the name ‘a’. Definitions are not of computation time must be independent expressions, at the same time expressions are of the programming language, compiler, also not treated as definitions. Definitions and computer used. As you aware that are distinct syntactic blocks. Definitions can algorithms are expressed using statements have expressions nested inside them, and of a programming language. If a bulk of vice-versa. statements to be repeated for many numbers 1.2.2 Parameters and arguments of times then subroutines are used to finish the task. Parameters are the variables in a function definition and arguments are Subroutines are the basic building the values which are passed to a function blocks of computer programs. Subroutines definition. are small sections of code that are used to 1. Parameter without Type perform a particular task that can be used repeatedly. In Programming languages these Let us see an example of a function subroutines are called as Functions. definition: 1 12th Computer Science_EM Chapter 1.indd 1 23-12-2022 15:42:16 https://csknowledgeopener.com When we write the type annotations (requires: b>=0 ) for ‘a’ and ‘b’ the parentheses are (returns: a to the power of b) mandatory. Generally we can leave out let rec pow a b:= these annotations, because it's simpler to if b=0 then 1 let the compiler infer them. There are times else a * pow a (b-1) we may want to explicitly write down types. This is useful on times when you get a type error from the compiler that doesn't make In the above function definition sense. Explicitly annotating the types can variable ‘b’ is the parameter and the value help with debugging such an error message. which is passed to the variable ‘b’ is the argument. The precondition (requires) and The syntax to define functions is close postcondition (returns) of the function to the mathematical usage: the definition is is given. Note we have not mentioned any introduced by the keyword let, followed by types: (data types). Some language compiler the name of the function and its arguments; solves this type (data type) inference then the formula that computes the image problem algorithmically, but some require of the argument is written after an := sign. If the type to be mentioned. you want to define a recursive function: use “let rec” instead of “let”. In the above function definition if expression can return 1 in the then branch, Syntax: The syntax for function definitions: shows that as per the typing rule the entire if expression has type int. Since the if expression is of type ‘int’, the function's let rec fn a1 a2... an := k return type also be ‘int’. ‘b’ is compared to 0 with the equality operator, so ‘b’ is also Here the ‘fn’ is used as a function a type of ‘int’. Since ‘a’ is multiplied with name. The names ‘a1’ to ‘an’ are variables another expression using the * operator, ‘a’ used as parameters. The keyword ‘rec’ is must be an int. required if ‘fn’ is to be a recursive function; otherwise it may be omitted. 2. Parameter with Type Now let us write the same function definition with types for some reason: Note A function definition which call (requires: b>=0 ) itself is called recursive function. (returns: a to the power of b ) let rec pow (a: int) (b: int) : int := For example: let us see an example to find the factorial of a number. if b=0 then 1 else a * pow a (b-1) XII Std Computer Science 2 12th Computer Science_EM Chapter 1.indd 2 23-12-2022 15:42:16 https://csknowledgeopener.com Interface is a description of all functions. (Requires: n>= 0) In our example, anything that "ACTS LIKE" let rec fact n := a light, should have function definitions like if n = 0 then 1 turn_on () and a turn_off (). The purpose of else interface is to allow the computer to enforce n ✳ fact(n-1) the properties of the class. The syntax for function types: The difference between interface and implementation is x→y x1 → x2 → y Interface Implementation x1 →... → xn → y Interface just Implementation defines what carries out the The ‘x’ and ‘y’ are variables indicating an object can instructions defined types. The type x → y is the type of a function do, but won’t in the interface that gets an input of type ‘x’ and returns an actually do it output of type ‘y’. Whereas x1 → x2 → y is a type of a function that takes two inputs, In object oriented programs classes are the first input is of type ‘x1’ and the second the interface and how the object is processed input of type ‘x2’, and returns an output of and executed is the implementation. type ‘y’. Likewise x1 → … → xn → y has type ‘x’ as input of n arguments and ‘y’ type 1.3.1 Characteristics of interface as output. The class template specifies the interfaces to enable an object to be created and Note operated properly. All functions are static An object's attributes and behaviour is definitions. There is no dynamic controlled by sending functions to the function definitions. object. 1.3 Interface Vs Implementation For example, let's take the example of increasing a car’s speed. An interface is a set of action that an object can do. For example when you press a light switch, the light goes on, you may not have cared how it splashed the light. In Object Oriented Programming language, an 3 Function 12th Computer Science_EM Chapter 1.indd 3 23-12-2022 15:42:16 https://csknowledgeopener.com let min x y z := ENGINE if x < y then if x < z then x else z else if y < z then y else z getSpeed 1.4 Pure functions required No Pure functions are functions which Pull Fuel speed will give exact result when the same arguments are passed. For example the Yes mathematical function sin (0) always results Return 0. This means that every time you call the function with the same arguments, you will always get the same result. A function can The person who drives the car be a pure function provided it should not doesn't care about the internal working. To have any external variable which will alter increase the speed of the car he just presses the behaviour of that variable. the accelerator to get the desired behaviour. Here the accelerator is the interface between Let us see an example the driver (the calling / invoking object) and the engine (the called object). let square x:= In this case, the function call would return: x * x be Speed (70): This is the interface. Internally, the engine of the car is The above function square is a pure doing all the things. It's where fuel, air, function because it will not give different pressure, and electricity come together to results for same input. create the power to move the vehicle. All of There are various theoretical these actions are separated from the driver, advantages of having pure functions. One who just wants to go faster. Thus we separate advantage is that if a function is pure, then interface from implementation. if it is called several times with the same Let us see a simple example, consider arguments, the compiler only needs to the following implementation of a function actually call the function once. Let’s see an that finds the minimum of its three example arguments: let length s:= i: = 0 if i

Use Quizgecko on...
Browser
Browser