Grade 12 Exam  >  Grade 12 Notes  >  Computer Science for Grade 12  >  Chapter Notes: Functions

Functions Chapter Notes | Computer Science for Grade 12 PDF Download

Revision Notes

  • The act of partitioning a program into individual components is called `Modularity'.
  • A module is a separately saved unit whose functionality can be reused at will. A Python module has the .py extension.
  • A Python module can contain objects like docstrings, variables, constants, classes, objects, statements, functions.
  • The Python modules that come preloaded with python are called "standard library modules".
  • A function is a named block of statements that can be invoked by its name.
  • Python can have three types of functions built-in functions, functions in modules and user-defined functions.
  • The docstrings are useful for documentation purpose.
  • Python module can be imported in a program using import statement.
  • There are two forms of import statements :
    (i) import < module name >
    (ii) from < module > import < object >
  • The built-in functions of Python are always available, one needs not import any module for them.
  • The math module of Python provides math functionality.
  • Python makes keyboard input easy, and as with many other techniques in Python. There are more than one way to enable keyboard input.
  • Making use of sys.stdin is the most widely used method to read input from the command line or terminal.
  • The command line sys.argv arguments are another way that we can grab input, and environment variables can also be used from within our programs.
  • The basic I/O (input/output) functions are print, raw_input( ), and input().
  • One of the most useful tools available in Python is the print function. This simply allows the program to display or print data for the user to read. For example :
    red=’Hi, how are you?’
    print red
  • raw_input( ) function reads a line from input (i.e. the user) and returns a string by stripping a trailing newline. The syntax is :
    mydata=raw_input('Prompt:')
    print (mydata) 
  • The input(x) function prints text to console and waits for number input, although the message giving is optional.
  • ceil(x): Return the ceiling of x as a float, the smallest integer value greater than or equal to x.
  • Floor(x) : Returns floor of x as a float, the largest integer value less then or equal to x.
  • fabs(x): Return the absolute value of x.
  • exp(x): Return e**x 
  • log(x,(base)):  With one argument, return the natural logarithm of x (to base e).
  • With two arguments, return the logarithm of x to the given base calculate as log(x)log(base)
  • log10(x): Return the base-10 logarithm of x. This is usually more accurate than log(x,10).
  • pow(x, y) : Return x raised to the power y. Exceptional cases follow Annex `F' of the C99 standard as far as 
  • possible. In particular. pow(l.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. if both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises Value Error.
  • sqrt(x): Return the square root of x.
  • cos(x): Return the cosine of x radians.
  • sin(x): Return the sine of x radians.
  • tan(x): Return the tangent of x radians.
  • degrees(x): Converts angle x from radians to degrees.
  • radians(x): Converts angle x from degrees to radians.
  • A function is a block of organized, reusable code that is used to perform a single, related action. Functions provides better modularity for your application and a high degree of code reusing.
  • Function blocks begin with the keyword def followed by the function name and parentheses ( ).
  • Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
  • The first statement of a function can be an optional statement - the documentation string of the function or docstring Ø The code block within every function starts with a colon (:) and is indented.
  • The statement return [expression] exit a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
  • Defining a function only gives a name, specifies the parameters that are to be included in the function, a structures the blocks of code.
  • You can call a function by using the following types of formal arguments :
    • Required arguments
    • Keyword arguments 
    • Default arguments 
    • Variable-length arguments
  • Required arguments are the arguments passed to a function in correct positional order.
  • Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.
  • A default argument is an argument that assumes a default value, if a value is not provided in the function call for that argument.
  • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
  • All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable.
  • The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python :
    • Global variables
    • Local variables
  • Variables that are defined inside a function body have a local scope, and those defined outside have a global
    Functions Chapter Notes | Computer Science for Grade 12
  • Built-in functions: Built-in functions are the functions that are built into Python and can be accessed by a programmer.
  • Examples of Some Built-in Functions
    (i) fabs():
    It returns the absolute value of a number.
    (ii) factorial(): This method finds the factorial of a positive integer.
    (iii) random(): It produces an integer between the limit arguments.
    (iv) today(): This method returns the current date and time.
    (v) search(): This function searches the pattern inside the string.
    (vi) capitalize(): It returns the copy of string in capital letters, 
  • String Functions 
    (i) Partition(): It splits the string at the first occurrence of the given argument and returns a 3-tuple.
    (ii) Join(): It takes a list of string and joins them as a regular string.
    (iii) Split(): It splits the whole string in the items with separator as a delimeter.
  • Modules: A module is a file containing Python definitions and statements. We need to import modules to use any containing part before separator, separator parameter and part after the separator if the separator parameter is found in the string of its function or variable in our code.
  • User-Defined Functions: We can also create our own functions. Parameters are the values provided in the parenthesis in the function header when we define the function. Arguments are the values provided in function call/invoke statement.
  • Function Arguments: You can call a function by using the following types of formal arguments: 
    •  Required arguments 
    • Keyword arguments 
    • Default arguments 
    • Variable-length arguments
The document Functions Chapter Notes | Computer Science for Grade 12 is a part of the Grade 12 Course Computer Science for Grade 12.
All you need of Grade 12 at this link: Grade 12
1 videos|25 docs|18 tests

Top Courses for Grade 12

1 videos|25 docs|18 tests
Download as PDF
Explore Courses for Grade 12 exam

Top Courses for Grade 12

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

study material

,

ppt

,

shortcuts and tricks

,

video lectures

,

Previous Year Questions with Solutions

,

Exam

,

pdf

,

Summary

,

Viva Questions

,

past year papers

,

practice quizzes

,

Semester Notes

,

Important questions

,

Extra Questions

,

Free

,

MCQs

,

Functions Chapter Notes | Computer Science for Grade 12

,

Functions Chapter Notes | Computer Science for Grade 12

,

Functions Chapter Notes | Computer Science for Grade 12

,

Objective type Questions

,

Sample Paper

;