Functions in C
Function
A function in C is a block of code which is used to perform specific tasks as many times as it is called, and is used to make coding simpler and convenient using a group of statement and operations. C function, this is a set of statements. there is a function in every single program. where function is required, function is called.
Note: int main() is also a function, every code has at least one function.
Advantages of function:-
- The code written in the function does not have to be written repeatedly.
- function protects programmer’s time and program space.
- Large program can be divided into small functions.
- The function can be called repeatedly where needed.
- The readability of the program increases.
Predefined function
- In-built functions are also called predefined or Library functions.
- In-built functions, different header files or processors have been created for each functions.
- There are lots of header files in C and they are grouped by different functions.
- If programmer wants to create your own header files too.
- Function declaration and definition is in header files.
Example:
- printf()
- scanf()
- strcpy()
User-defined function
User defined functions are those functions which programmer (you) create for yourself. Programmer can create as many functions as per your requirement.
Declaration of a function
When writing a function we need to tell the computer the name, the return type and the parameter(if any) of that function.
return-type – What kind of value will return when your function execution is complete, you define it by return type. If you are creating an addition program which adds 2 whole numbers then your return type is int.
function-name – this is the name of your function. These should be unique in the whole program. When you call the function, you write this name only.
list-of-parameters – These are the lists of variables that you will pass when calling the function. For example, if you are creating addition of function then you can pass 2 numbers or 2 variables as parameters, and then add them inside the function and show results. It is not necessary that you define parameters in all functions.
One thing you should always remember is that the function declaration statement is terminated from semicolon. But this does not happen with the function definition.
Function Definition
Function definition includes return-type, function-name, and list-of-parameters as in the function declaration. After that, those statements are written in the block of curly brackets that you want to execute.
Let’s see how
#include<stdio.h> int add(int a,int b) //a,b are the parameters which can be provided while calling the function { int c; c=a+b; return c;//returns the integer value of the sum here }
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
I have paid for combo 2 pack… its actually getting difficult to access the materials…. i have to always access through the link only why my profile is not being saved as paid it should have saved automatically right its actually trouble some to use the website inspite the money….. its been more than 10 days now I cannot even ask for refund…. the materials are also not satisfactory or any any better than the free online materials……… please for on the website make it a bit sorted……….. I am spending whole time in confusion while using the website……
Hey Swarna, you might be finding it difficult because of the wrong links that you are going through, please contact our mentors, they’ll help you out in understanding the process 8884209544
8882457079
where can i find practice questions related to every topic?
We will be linking them on our C/C++ course page very soon
file handling concept is not there
Hey Yogesh
We have a different page for file handling
Here it is – https://prepinsta.com/c-plus-plus-theory/files-and-streams/
where is call by reference example??
please visit – https://prepinsta.com/all-about-c-language/call-by-reference/ this page Madhavi, here you will find all the information related to call by reference