0% found this document useful (0 votes)
2 views14 pages

Function

The document explains the concept of functions in C programming, detailing their definition, types (library and user-defined), and structure including declaration, definition, and calling. It covers the differences between formal and actual arguments, return types, and the advantages of using functions to modularize code. Additionally, it discusses the methods of passing arguments to functions, namely call by value and call by reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Function

The document explains the concept of functions in C programming, detailing their definition, types (library and user-defined), and structure including declaration, definition, and calling. It covers the differences between formal and actual arguments, return types, and the advantages of using functions to modularize code. Additionally, it discusses the methods of passing arguments to functions, namely call by value and call by reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
FUNCTION A function is a self contained block of codes or sub programs with a set of statements that perform some specific task or coherent task when it is called. Itis something like to hiring a person to do some specific task like, every six months servicing a bike and hand over to it. Any ‘C’ program contain at least one function i.e main(). There are basically two types of function those are 1. Library function 2. User defined function The user defined functions defined by the user according to its requirement System defined function can’t be modified, it can only read and can be used. These function are supplied with every C compiler Source of these library function are pre complied and only object code get used by the user by linking to the code by linker Here in system defined function description: Function definition : predefined, precompiled, stored in the library Function declaration : In header file with or function prototype. Funetion call : By the programmer User defined function Syntax:- Retum type name of function (type 1 arg 1, type2 arg2, type3 arg3) Return type function name argument list of the above syntax So when user gets his own function three thing he has to know, these are. Function declaration Function definition Function call These three things are represented like int function(int, int, int); /*function declaration*/ main() — /* calling function*/ { function(arg] ,arg2,arg3); } int function(type I arg 1,type2 arg2,type3, arg3)_/*function definition/* i Local variable declaration; Statement; Return value; } Function declarati Function declaration is also known as function prototype. It inform the compiler about three thing, those are name of the function, number and type of argument received by the function and the type of value returned by the function. While declaring the name of the argument is optional and the function prototype always terminated by the semicolon. Function definit Function definition consists of the whole description and code of the funetion Ittells about what function is doing what are its inputs and what are its out put It consists of two parts function header and function body Syntax:- return type function(type 1 argl, type2 arg2, type3 arg3) /*function header*/ { Local variable declaration; Statement 1; Statement 2; Return value } The return type denotes the type of the value that function will return and it is optional and if it is omitted, it is assumed to be int by default. The body of the function is the compound statements or block which consists of local variable declaration statement and optional return statement. The local variable declared inside a function is local to that function only. It can’t be used anywhere in the program and its existence is only within this function. The arguments of the function definition are known as formal arguments. Function Call When the function get called by the calling function then that is called, function call. The compiler execute these functions when the semicolon is followed by the function name. Example:- function(argllarg2,arg3); The argument that are used inside the function call are called actual argument Ex int S=sum(a, b); Hactual arguments Actual argument The arguments which are mentioned or used inside the function call is knows as actual argument and these are the original values and copy of these are actually sent to the called function It can be written as constant, expression or any function call like Function (x); Function (20, 30); Function (a*b, cd); Function(2,3,sum(a, b)); Formal Arguments The arguments which are mentioned in function definition are called formal arguments or dummy arguments. These arguments are used to just hold the copied of the values that are sent by the calling function through the function call. These arguments are like other local variables which are created when the function call starts and destroyed when the function ends. The basic difference between the formal argument and the actual argument are 1) The formal argument are declared inside the parenthesis where as the local variable declared at the beginning of the function block. 2). The formal argument are automatically initialized when the copy of actual arguments are passed while other local variable are assigned values through the statements. Order number and type of actual arguments in the function call should be match with the order number and type of the formal arguments. Return type It is used to return value to the calling function. It can be used in two way as return Or —_return(expression); Ex:- return (a); return (a*b return (a*b+c); Here the 1* return statement used to terminate the function without returning any value Ex: /*summation of two values*/ int sum (int al, int a2); main() int a,b; printf(“enter two no”); seanf("%d%d”,&a,&b); int S=sum(a,b); printf((“summation is = %d",s); } int sum(intx! int y1) { int =x1+y1; Return z; } Advantage of function By using function large and difficult program can be divided in to sub programs and solved. When we want to perform some task repeatedly or some code is to be used more than once at different place in the program, then function avoids this repeatition or rewritten over and over. Due to reducing size, modular function it is easy to modify and test Notes:- C program is a collection of one or more function. A function is get called when function is followed by the semicolon. A function is defined when a function name followed by a pair of curly braces Any function can be called by another function even main() can be called by other function. main() { function1() } fanetion1() { Statement; fanction2; } fnction 2() { } So every function in a program must be called directly or indirectly by the main() function. A function can be called any number of times. ‘A function can call itself again and again and this process is called reeursion. A function can be called from other function but a function can’t be defined in another function Category of Function based on argument and return type i) Function with no argument & no return value Function that have no argument and no return value is written as:- void function(void); main() { void function() { Statement; } Example:- void me(); main() { me(); printf(“in main”); } void me() { printf(“come on”); } Output: come on inn main ii) Function with no argument but return value Syntax: int fun(void); main() { int r; r=funQ; int fun() { reum(exp); } Example:- int sum(; main() { int b=sum0; printi(“entered %d\n, b”); } int sum() { int a,b,s; s=atb; return s; } Here called function is independent and are initialized. The values aren’t passed by the calling function Here the calling function and called function are communicated partly with each other. ) function with argument but no return value Here the function have argument so the calling function send data to the called function but called function dose n’t return value. Syntax:- void fun (int,int); main() { int (a,b); } void fun(int x, int y); { Statement; } Here the result obtained by the called function. iv) function with argument and return value Here the calling function has the argument to pass to the called function and the called function returned value to the calling function. Syntax:- fan(int,int); main() { int r=fun(a,b); } int fun(intx,inty) { return(exp); } Example: main() { int fun(int); int a,num; printf(“enter value:\n”); seanf(“%d",&a) int num=fan(a); I int fun(int x) { +x; return x; Call by value and call by reference There are two way through which we can pass the arguments to the function such as call by value and call by reference 1. Call by value In the call by value copy of the actual argument is passed to the formal argument and the operation is done on formal argument. When the function is called by ‘call by value’ method, it doesn’t affect content of the actual argument. Changes made to formal argument are local to block of called function so when the control back to calling function the changes made is vanish. Example:- main() { int xy; change(int,int); printf(“enter two values:\n”); seanf(“%d%d”,8x,&y); change(x ,y); printf(‘*value of x=%d and y=od\n”,x .y); } change(int a,int b); Output: enter two values: 12 23 Value of x=12 and y=23 2. Call by reference Instead of passing the value of variable, address or reference is passed and the function operate on address of the variable rather than value. Here formal argument is alter to the actual argument, it means formal arguments calls the actual arguments. Example: void main() int a,b; change(int *,int*); printf(‘“enter two values:\n”); scanf{“%d%d",&a,&b); change(&a,&b); printf(“after changing two value of a=Y%d and b=%din:”a,b); change(int *a, int *b) { int k; koa; *a=*b; *b=k; printf(“value in this function a=%d and b=%d\n”,*a,*b); } Output: enter two values: 12 32 Value in this function a=32 and b=12 After changing two value of a=32 and b=12 So here instead of passing value of the variable, directly passing address of the variables. Formal argument directly access the value and swapping is possible even after calling a function.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy