PDF Google Drive Downloader v1.1


Report a problem

Content text unit4-function-notes .docx

Unit-4 1 Function A function is a group of statements / instructions to perform some specific tasks when it’s called. It is the basic building block of a C program that provides modularity and code reusability. They are also called subroutines or procedures in other languages. Syntax of Functions in C or structure of function The syntax of function can be divided into 3 aspects: Function Definition Function Calls Function Declaration Function Definition The function definition consists of actual statements which are executed when the function is called. The programming statements of a function are enclosed within { } braces along with its header as shown below, Syntax:- return_type function_name (zero or arguments/parameters list) { // body of the function } Function Call or Calling a function
Unit-4 2 A function call is a statement that instructs the compiler to execute the function. We use the function name and parameters in the function call. Syntax:- name_of_the_function (zero or argument values/parameter values); Variable = function _name(zero or argument values/parameter values); A function can be called by specifying its name, followed by list of arguments enclosed within the parenthesis. The arguments should be separated by commas. If the function does not pass any arguments, then an empty pair of parenthesis should follow the name of the function. Example:- #include <stdio.h> void sum( ) { int a=10,b=20,c; c=a+b; printf(“The addition of 10 and 20 number is :%d”,c); } int main( ) { // function call sum( ); return 0; } Function Declarations In a function declaration, we must provide the function name, its return type, and the number and type of its parameters. A function declaration tells the compiler that there is a function with the given name defined somewhere else in the program. Syntax return_type name_of_the_function( zero or datatype arguments/parameters); The parameter name is not mandatory while declaring functions. We can also declare the function without using the name of the data variables. Example int sum(int a, int b); // Function declaration with parameter names int sum(int , int); // Function declaration without parameter names
Unit-4 3 Note: A function in C must always be declared globally before calling it. Only one value can be returned from a C function. To return multiple values, we have to use pointers or structures. Types of Functions There are two types of functions in C: Library Functions User Defined Functions 1. Library Function
Unit-4 4 A Library function is also referred to as a “built-in function”. A compiler package already exists that contains these functions, each of which has a specific meaning and is included in the package. Built-in functions have the advantage of being directly usable without being defined, whereas user-defined functions must be declared and defined before being used.  For Example: main(),pow(), sqrt(), strcmp(), strcpy() etc. Advantages of C library functions C Library functions are easy to use and optimized for better performance. C library functions save a lot of time i.e, function development time. C library functions are convenient as they always work. Example: #include <math.h> #include <stdio.h> int main( ) { Float Number; Number = 49; // Computing the square root with the help of predefined C library function Float squareRoot = sqrt(Number); printf("The Square root of %.2f= %.2f”,Number, squareRoot); return 0; } Output The Square root of 49.00 = 7.00 2. User Defined Function Functions that the programmer creates are known as User-Defined functions ,User- defined functions can be improved and modified according to the need of the programmer. Whenever we write a function that is case-specific and is not defined in any header file, we need to declare and define our own functions according to the syntax. Advantages of User-Defined Functions -functions can be modified as per need. -The Code of these functions is reusable in other programs. -These functions are easy to understand, debug and maintain. Syntax:- return_type user_defined_function_name (zero or arguments/parameters list)

Related document

x
Report download errors
Report content



Download file quality is faulty:
Full name:
Email:
Comment
If you encounter an error, problem, .. or have any questions during the download process, please leave a comment below. Thank you.