C FUNCTIONS
Topics:
- Overview of Function
- Declaring a Function
- Defining a Function
- Return Statement
C language |
Overview of Function
Meaning
- A function is a self-contained program that carries out some specific, well-defined task. Every C program consists of one or more functions.One of these functions must be called main. Program execution will always begin by carrying out the instructions in main. Additional functions will be subordinate to main, and perhaps to one another.
- Program containing multiple functions may appear in any other, but must be independent of one another. i.e. A function definition cannot be embedded within another.
- A function will execute instruction whenever called from some other portion of the program. The same function can be accessed from several different places within a program. After executing its instruction, control will be returned to the point from which the function was accessed.
- A function will process information passed to it from the calling portion of the program, and return a value.
Purpose
- The use of programmer-defined functions allows a large program to be broken down into a number of smaller, self-contained components, each of which has some unique, identifiable purpose. So, a C program can be modularized through the intelligent use of such functions.
Importance
- Many programs require a particular group of instructions to be accessed repeatedly from several different places within the program. The repeated instructions can be placed with in a single function, which can then be accessed whenever it is needed. Moreover, a different set of data can be transferred to the function each time it is accessed. Thus, the use of a function avoids the need for repeated programming of the same instruction.
- Breaking the program into several concise function results in logical clarity, where each function represents some well-defined part of the overall problem.
- The use of function enable a programmer to build a customized library of frequently used routines. Each routine can be programmed as a separate function and stored within a special library file, and can be accessed and attached to the program during the compilation process whenever required. This avoids repetitive programming between programs.
- Lengthy and complicated are easier to write and easier to debug.
- These programs are easy to maintain, because if any modification is required, it can be confined to certain parts of the program.
- Such programs are more readable and can be well documented.
Program Calling A User-Defined Function
Program-1
Return Statement
#include<stdio.h>
/* Program Calling A User-Defined Function */
void main ()
{ printf("\nThis comes from the main program\n\n");
call( );
printf("\nThis again comes from the main program\n");
}
call( )
{ printf("%60s\n", "This comes from the called program");
return;
}
Declaring a Function
- A function is declared by specifying the type of value it returns and by adding a pair of parentheses after the function name.
Format:
type returned function name ( );
Example:
in square( );
Explanation
- Square( ) is declared to be the name of a function
- It is defined elsewhere in the program
- It returns an int value
Same Function called twice to get 2 values
Program-2#include<stdio.h>
/* Same Function called twice to get 2 values */
void main ()
{ int length,width;
printf("Enter the length of a rectangle:");
length = get_int( );
printf("Enter the width:");
width = get_int( );
printf("\nThe area is %d", length * width);
}
get_int( )
{ int numb;
scanf("%d",&numb);
return(numb);
}
Defining a Function
- A function definition has 3 components :
- The first line
- The argument declaration
- The body of the function
The first line of a function definition contains the type of the value returned by the function, followed by the function name, and a set of arguments, separated by commas and enclosed parentheses.
Format:
data-type name (formal argument 1, formal argument 2, ... formal argument n)
- The formal argument allow information to be transferred from the calling portion of the function. They are know as parameters (The corresponding arguments in the function reference are called actual argument since they define the information actually being transferred) The identifiers used as formal arguments are local to a function. Hence, the names of the formal arguments may be the same as the names of other identifiers that appearoutside the function definition.
- The argument declarations follow the first line. All arguments must be declared at this point in the function. Each formal argument must have the same data type as its corresponding actual argument. That is, each formal argument must be of the same data type as the data item it receives from the calling portion of the program.
- The remainder of the function definition is a compound statement (also referred as body of the function) that defines the action to be taken by the function. This statement can contain expression statements, other compound statements, control statements, it can access other functions or even access itself.
Convert a lower-case character to upper-case, Using a Function
Program-3#include<stdio.h>
/* Convert a lower-case character to upper-case, Using a Function */
void main ()
{ char lower, upper;
char lower_2_upper(char lower);
printf("Enter a Lower-Case Character : ");
scanf("%c",&lower);
upper=lower_2_upper(lower);
printf("\nThe Upper-Case Is : %c\n",upper);
}
char lower_2_upper(char ch1)
{ char ch2;
ch2=(ch1 >='a' && ch1 <= 'z') ? ('A' + ch1 - 'a') : ch1;
return(ch2);
}
Return Statement
Exits immediately from the currently executing function to the calling routine, optionally returning a value.
Information is returned from the function to the calling portion of the program via the return statement. The return statement also causes control to be returned to the point from which the function was accessed.
Format:
return [ <expression> ] ;
Example:
double sqr(double x)
{
return (x * x);
}
- The value of the expression is returned to the calling portion of the program. The expression is optional. If the expression is omitted, the return statement simply causes control to revert back to the calling portion of the program, without any information transfer.
- A function can return only one value to the calling portion of the program.
- A function definition can include multiple return statements, each containing a different expression.
- A function that does not returns a value is called a void( ) function. If a function has been declared to be of type void and it returns a value an error will occur. A program should also not use the value from a void( ) function.
Program to Calculate Square of a given number. Function evaluating an expression
Program-4
#include<stdio.h>
/* Program to Calculate Square of a given number. Function evaluating an expression */
void main ()
{ int square, number;
printf("\nEnter a number: ");
scanf("%c",&number);
square = calc(number);
printf("\nThe square of the given number is : %c\n",square);
}
calc(number)
int number;
{ return (number * number); }
Program to Test for a leap year. Function evaluating a complex expression
Program-5
#include<stdio.h>
/* Program to Test for a leap year. Function evaluating a complex expression */
void main ()
{ int year;
printf("Please enter a year: ");
scanf("%d",&year);
if(check_leap(year))
printf("%d Is a leap year .\n",year);
else
printf("%d Is not a leap year.\n",year);
}
check_leap(year)
int year;
{ return(year % 4 = = 0 && year % 100 != 0 || year % 400 = = 0); }
Function returning two different values
Program-6
#include<stdio.h>
/* Function returning two different values */
void main ()
{ char lower, upper;
char lower_2_upper(char lower);
printf("Enter a Lower-Case Character : ");
scanf("%c",&lower);
upper=lower_2_upper(lower);
printf("\nThe Upper-Case Is : %c\n",upper);
}
char lower_2_upper(ch1)
char ch1;
{ if(ch 1 >= 'a' && ch1 <= 'z')
return('A' + ch1 - 'a');
else
return(ch1);
}
If you find this article informative, please share the post to your friends.
******************
Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).
- Learn how to write the Turbo C program step-by-step | Lesson -1
- Learn how to write the Turbo C program step-by-step | Lesson -2
- Learn how to write the Turbo C program step-by-step | Lesson -3
- Learn how to write the Turbo C program step-by-step | Lesson -4
Click below for other blogs...
- Important Terminology of Computer Science
- Revolution in Information Technology
- How to delete WhatsApp data from Google drive
- Most useful website list for job seekers
- Transfer data from PC to phone | now you can UNDO Gmail's sent mail on smartphone too
- Do you know how to delete Facebook account
- What is TCP/IP Reference Model
- How to get the right results in Google search
Thanks a lot for reading.
Neel Kamal
Learn how to write the C program step-by-step | Lesson - 4
Reviewed by Neel Kamal
on
November 17, 2020
Rating:
No comments:
For More Details Subscribe & Comment..!