Function In 'C'
- Till now we have seen many library function that perform some operations or some calculation.
- But 'C' language also allows programmer to define their own function to solve certain task.
- Programmer define functions are self contained block which perform certain task.
- Advantage of programmer define function is that large program can break into small and clean self-contained blocks which performs certain task.
- Using programmer define function in a larger program keeps program clean and more readable ,and also it reduce the complexity of the program.
Concept of Functions
- As functions are programmer define function ,which is a self-contained block which perform some well defined tasks.
- Functions in 'C' language are define outside the main() and they are used in main().
- For defining function, you have to first declare it on the top.
- As the function has completed it's task the control goes at that point where it was called or Used.
- Generally the function is defined such a manner that it may or may not take some arguments and return a single value or not.
- Taking argument and returning value ,totally depends upon the programmer or according to the need whether the certain program needs an argument or not and whether it will return a value or not.
- For example: printf() this a function which take an argument and returns nothing.
getchar() this function does not take any argument, but it returns a value which is a character. - Function can return only single Value.
Types of functions
- As you know functions can take arguments and may return a value ,so according this there are four types of functions.
- Functions which takes nothing and returns Something:
- Functions which takes something and returns Nothing:
This type of function takes something as argument but returns Nothing.
For example: printf() this a function which take an argument and returns nothing.
- Functions which takes something and returns Something:
This type of function takes something as argument and returns Something.
For example: power(a,n) this a function which take an argument and also returns some value.
- Functions which takes Nothing and returns Nothing:
This type of function does not takes an argument and also returns Nothing.
This type of function takes nothing as argument but returns Something.
getchar() this function does not take any argument,but it returns a value which is a character.
Declaration of Function
- Declaration of function is also called as function prototype
- Before defining a function ,you have to declare it on the top
- As you know function can take an argument an may or may not return a value, this think we have to specify at the declaration of function.
- let's see ,how we can declare a function.
Syntax:
return_datatype function_name(datatype arg1_name, datatype agr2_name,....,);
//return_datatype :it means which type of data function will return.
//arg :no of arguments depends upon the programmer,
//that how much data he wants to complete the task.
return_datatype function_name(datatype arg1_name, datatype agr2_name,....,); //return_datatype :it means which type of data function will return. //arg :no of arguments depends upon the programmer, //that how much data he wants to complete the task.
Note: Arguments are nothing just an mode of communication between the function and the calling function.
Argument are enclosed in parenthesis('()') contain variable names seperated by commas .
Lets declare a function whose name is 'Add' which will take two integer values as an argument and it will return an integer value
Solution:
int Add(int a,int b);
//this decleration means,
//function will take 2 arguments,
//and return an integer value.
int Add(int a,int b); //this decleration means, //function will take 2 arguments, //and return an integer value.
Q:declare a function whose name is 'temp' which will take One float type value as an argument and it will return an float type value
Solution:
float temp(int a);
//this declaration means,
//function will take 1 argument,
//and return an float value.
float temp(int a); //this declaration means, //function will take 1 argument, //and return an float value.
Q:declare a function whose name is 'give_ascii' which will take An character as an argument and it will return an integer value
Solution:
int give_ascii(char a);
//this declaration means,
//function will take 1 argument,
//and return an integer value.
int give_ascii(char a); //this declaration means, //function will take 1 argument, //and return an integer value.
Defining a Function
- Defining a function is the process, where we write a actual code which will perform
certain task - As you have a declare function at the top, the same thing you have to write while defining the function.
- First line contain the function prototype.
- There is no semicolon at the end of prototype, while definition of function. s
- Remember function can return only single value.
- let's see ,how we can define a function.
- We will write a function for adding two numbers and returning the result.
int add(int a ,int b) {//defination of function. int result; result = a + b; return result; //returning value of result. }
- Defining the Function for checking number is even or odd.
int check(int a) { int result; if(a % 2==0) result = 1; else result = 0; //if result is 1 then number is even. //if result is 0 then number is odd. return result; //returning the value of result. }
Further Concepts:
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- Practice programs
Further Topics:
- What are Pointers?
- how to declare pointers?
- how to initialize a pointers?
- How to access the pointer variables?
- Function of '*' and '&' operators
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- How to declare Structure Pointer?
- Working with structure Pointer
- How to Create Structure Variable Dynamically ?
- Creating structure variable through malloc() and calloc()
People Also Searched:
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() functions
- Strings In C
- Concept of strings
- Concept of Initializing One Dimensional Array
- How to take array elements as input from the User
- How to declare 1D Array?
- How to Initialize 1D Array?
- What are Array?
- Need Of Array
- Why Array?
- Concept of Array
0 Comments
Post a Comment