0% found this document useful (0 votes)
7 views7 pages

C Programming Functions Guide

The document provides an overview of C programming functions, including definitions, types (standard library and user-defined), and their components. It includes sample code demonstrating function creation, calling, and various looping structures. Key points about function characteristics and execution flow in C are also highlighted.

Uploaded by

keynlozano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

C Programming Functions Guide

The document provides an overview of C programming functions, including definitions, types (standard library and user-defined), and their components. It includes sample code demonstrating function creation, calling, and various looping structures. Key points about function characteristics and execution flow in C are also highlighted.

Uploaded by

keynlozano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

ICT 8 QUARTER 3 REVIEWER //function call//

-C program functions-

What is function?
-block of code that performs a specific
task.

Two function to solve


-create a circle function
-create a color function

Two types of function


Standard library
function
-the standard library function are built in c
programming.
-defined in header files.
-the printf is a standard library function to
send formatted output to the screen.
(display output to the screen). This
function is defined in the stdio.h header
file.
User-defined function
-function created by the user
-advantages of user-defined function-
-the program will be easier to understand,
maintain, and debug.
-reusable quotes that can be used in other
programs.
-a large program that can be divided in
smaller modules. Hence, a large project
can be divided among many programs.
Example code of user-defined
function: # include <stdio.h>
//function prototype//
In addnumbers (int a, int
b); Int main () {
Int n1, n2, sum;
Printf(“enters two -C program to show function definition after
numbers:”); Scanf(“%d, main- #include <stdio.h>
%d”, &n1, &n2); #include <conio.h>
Sum=addnumbers (n1, /*function definition of their main in this
case function declaration is required*/
n2); Printf(“sum=%d”,
Int getsum (int a, int b);
sum); Return 0;
}
//function definition//
Int addnumbers (int a, int b);
//return
statement// Int
result;
Int a+b;
Return
result;

-Parts of a Function-
Return type- data type of the value
returned by the function. Void data type
is used when the function does not
returned any value.
Function_name – representing the name of
function (must have unique names).
Type arg1, type arg2… - comma
separated list of data type and names of
parameters passed by the calling
function.
Body of a Function – contains a set of
statement that will be executed when its
function called.
Return (expression) – used to send a value
to calling function before fermination of
function.
-syntax of function definition-
Return_type function_name (type arg 1,
type arg 2….) {
Body of a function
Return (expression);
}
Int main () Printf(“5. Exit\n”);
{ Int a,b, Printf(“Enter a choice from 1-5: \
sum; n”); Scanf (“%d”, choice);
Printf(“enter 2 numbers
\n”); Scanf (“%d %d”,
&a, &b);

/*calling getsum
function*/ Sum=
getsum (a,b); Printf(“
sum = %d”, sum);
Getch ();
Return 0;
}
Int getsum (int a, int b)
{ Return a+b;
}

Sample Program of Function

# include <stdio.h>
#include <math.h>
Float aos (float
side);
Float aor (float length, float
width); Float aoc (float radius);

Float aot (float base, float


height); Int main () {
Int choice = 0;
Float side, length, width, radius, base,
height, area;
While (choice! = 5) {
Printf(“\n Area Calculation Menu\n”);
Printf(“1. Square: \n”);

Printf(“2. Rectangle \n”);


Printf(“3. Circle \n”);
Printf(“4. Triange\n);
If (choice == 1) { * radius*radius; }
Printf(“Enter the length of the Float aot (float base, float height) {
square:”) Scanf(“%f”, &side); Return 0.5 * base * height; }
Printf(“The area of the square is: %2f\
n”, area);
} else if (choice == 2) {
Printf (“Enter the length of the
triangle”); Scanf (“%f”,
&length);
Printf(“Enter the width of the
rectangle:”); Scanf(“%f”, &width);

Area = aor (length, width);


Printf(“The area of the rectangle is
%2f\n”, area); Else if (choice == 3) {
Printf(“Enter the radius of
the circle:”); Scanf(“%f”,
&radius);

Area = aoc (radius);


Printf(“The area of the circle is %2f\n”,
area); } Else if (choice == 4) {
Printf(“Enter the base of the triangle:
“); Scanf(“%f”, &base);
Area = aot (base, height);
Printf(“the area of the triangle is: %2f\
n”, area); } Else if (choice == 5) {
Printf(“Existing the
program!”); } Else {

Printf(“invalid choice! Please select


valid option); }}
Float aos (float
side) { Return
side*side; }
Float aor (float length, float
width) { Return
length*width; }
Float aoc (float
radius) { Return 3.14
C programming function facts Int main()
1. all c programs must have contain a { Int mv, I;
main function ()
2. the operating system cost main
function of a program from where
programs execution starts.
3. one function can call another function.
4. C function can be called with or without
arguments depending on a function
declaration.
We can create any number of function in c
program.
6. c function can be evoked from anywhere
within a c program.
7. we can call a function any number of
times in c program.
8. function name should be unique
9. a function declaration in c tells the
compiler about function name. function
parameters and return value of a
function.
10.c function can return only one value
to the calling function.
11. c function may or may not return
values to calling function depending on the
return type defined in function declaration.
Void data type is used when the function
does not return any value. By default the
return type of function is integer (int)
12.there are two types of function in c,
library function and user defined
function.
13.well function terminates, program
control is returned to the function or from
where it is called.
14.execution of c program comes to
an end when there is no function or
statement left to execute in main
function body.

Sample Looping Code

/”For Loop Maximum Value”/


#include <stdio.h>
Printf(“Enter a maximum value:
“); Scanf(“%d”, &mv);
Printf(“\n The odd numbers are: \
n”); For (I = 1; I <= mv; I += 2)
{
Printf(“%d “, i); }
Printf(“ \n \n The even numbers are: \
n”); For (I = 2; I <= mv; I += 2) {
Printf(“%d “, i); }
Return 0;

/”While Loop Maximum Value”/


#include <stdio.h>
Int main()
{ Int mv, I;
Printf(“Enter a maximum value:
“); Scanf(“%d”, &mv);
Printf(“\n The odd numbers are: \
n”); I=1;
While ( I <= mv) {
Printf(“%d “,
i); I+=2; }
Printf(“ \n \n The even numbers are: \
n”); I=2;

While ( I <= mv) {


Printf(“%d “,
i); I+=2; }
Return 0;
}

/”Do While Loop Maximum


Value”/ #include <stdio.h>
Int main()
{ Int mv, I;
Printf(“Enter a maximum value:
“); Scanf(“%d”, &mv);
Printf(“\n The odd numbers are: \
n”); I=1;
Do {
Printf(“%d “, i);
I+=2; }
While ( I <= mv);
Printf(“ \n \n The even numbers are: \
n”); I=2;

Do {
Printf(“%d “, i);
I+=2; }

While ( I <= mv);


Return 0;

You might also like