"HARD WORK BEATS TALENT WHEN TALENT DOSEN'T WORK HARD."
"HARD WORK BEATS TALENT WHEN TALENT DOSEN'T WORK HARD."
A function is self-contained group or block of statement to perform a specific operation. In Every C program by default function is main function. You can create multiple function for multiple logics. How you create function it's up to you.
A function declaration tells the compiler about a function's name, return type, and parameters.
Function definition contain logics of the program. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.
return_type function_name(parameter optional) { body of statement }
A function may return a value or may not. It depend on function data type. Whether you are using int or void. Here void data type return nothing and int data type return 0.
This is the actual name of function. Like writing main it is a function name.
Parameter are optional. It's up to you whether it required or not.
Predefine function also called inbuilt function. Which are reserved in program.
Predefine function | Use of Predefine function |
---|---|
printf(); | It is use to display or print the output to the console. |
scanf(); | It is use to scan or accept the input from keywords. |
clrscr(); | Use to clear the output screen. |
getch(); | It holds the output screen until a key is pressed. |
#include<stdio.h> #include<conio.h> void main() { clrscr(); printf("Welcome to Campuslife"); getch(); }
Below is the format of printf() function. Only double quote is mandatory in printf() function. Rest thing is optional or based on program requirement.
printf("control string with format specification",argument1, argument2....argument n);
In below example Hello is a message and it will show on the console screen when you execute your program.
printf("Hello"); //Output will show as Hello.
int a = 10; printf("%d",a); //Output will show as 10.
int a = 10; printf("The result is %d",a); //Output will show as The result is 10
In Example 3 (The result is) is a control string.
In Example 3 (%d) is a placeholder nothing but format specification of integer value. To display the value we need to hold the some place for that.
In Example 3 "a" variable is an argument.
Data type | format specification/place holder |
---|---|
short/int | %d |
long | %ld |
float | %f |
double | %lf |
char | %c |
string | %s |
unsigned | %u |
octal | %o |
hexadecimal | %x |
User define function are those function which is define by the user not by the program. In below example we have created two function that are define by our self.
//user define function 1 void sum() { int a=10,b=20,c; c=a+b; printf("Result is %d",c); } //calling the user define function in main function. void main() { clrscr(); sum(); gethc(); }