0% found this document useful (0 votes)
50 views

Function

1. A function is a block of code that performs a specific task. Functions allow programmers to break large programs into smaller, reusable parts. 2. There are four types of functions: functions with no arguments and no return value, functions with no arguments but a return value, functions with arguments but no return value, and functions with arguments and a return value. 3. Functions make programs easier to write, read, update, debug and reuse code. Functions can take in arguments from the calling function, perform a task using those arguments, and return a value.

Uploaded by

Burhan Pasha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Function

1. A function is a block of code that performs a specific task. Functions allow programmers to break large programs into smaller, reusable parts. 2. There are four types of functions: functions with no arguments and no return value, functions with no arguments but a return value, functions with arguments but no return value, and functions with arguments and a return value. 3. Functions make programs easier to write, read, update, debug and reuse code. Functions can take in arguments from the calling function, perform a task using those arguments, and return a value.

Uploaded by

Burhan Pasha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Function

A large program in c can be divided to many subprogram

The subprogram posses a self contain components and have well define purpose.

The subprogram is called as a function

Basically a job of function is to do something

C program contain at least one function which is main().

Advantages of function
It is much easier to write a structured program where a large program can be divided into
a smaller, simpler task.

Allowing the code to be called many times

Easier to read and update

It is easier to debug a structured program where there error is easy to find and fix
1: #include <stdio.h> Arguments/formal parameter
 Function names is cube
2:  Variable that are requires is
3: long cube(long x); long
4: Return data type  The variable to be passed on
5: long input, answer; is X(has single arguments)—
6: value can be passed to
7: int main( void ) function so it can perform the
8: { specific task. It is called
Actual parameters
9: printf(“Enter an integer value: ”);
10: scanf(“%d”, &input);
11: answer = cube(input);
12: printf(“\nThe cube of %ld is %ld.\n”, input, answer);
13: Output
14: return 0;
15: } Enter an integer value:4
16:
17: long cube(long x)
18: { The cube of 4 is 64.
19: long x_cubed;
20:
21: x_cubed = x * x * x;
22: return x_cubed;
23: }

How the function works


C program doesn't execute the statement in function until the function is called.

When function is called the program can send the function information in the form of one
or more argument.

When the function is used it is referred to as the called function

Functions often use data that is passed to them from the calling function

Data is passed from the calling function to a called function by specifying the variables in
a argument list.

Argument list cannot be used to send data. Its only copy data/value/variable that pass
from the calling function.

The called function then performs its operation using the copies.
Function prototypes
Provides the compiler with the description of functions that will be used later in the
program

Its define the function before it been used/called

Function prototypes need to be written at the beginning of the program.

The function prototype must have :

A return type indicating the variable that the function will be return

Syntax for Function Prototype

return-type function_name( arg-type name-1,...,arg-type name-n);

Function Prototype Examples

 double squared( double number );

 void print_report( int report_number );

 int get_menu_choice( void);

Function Definitions
It is the actual function that contains the code that will be execute.

Should be identical to the function prototype.

Syntax of Function Definition

return-type function_name( arg-type name-1,...,arg-type name-n) ---- Function header

declarations;

statements;

return(expression);

}
Function Definition Examples

float conversion (float celsius)

float fahrenheit;

fahrenheit = celcius*33.8

return fahrenheit;

The function name’s is conversion

This function accepts arguments celcius of the type float. The function return a float value.

So, when this function is called in the program, it will perform its task which is to convert
fahrenheit by multiply celcius with 33.8 and return the result of the summation.

Note that if the function is returning a value, it needs to use the keyword return.

Function return types


Can be any of C’s data type:

char

int

float

long………

Examples:

int func1(...) /* Returns a type int. */

float func2(...) /* Returns a type float. */

void func3(...) /* Returns nothing. */


Types of Functions
Function can be divided into 4 categories:

 A function with no arguments and no return value


 A function with no arguments and a return value
 A function with an argument or arguments and returning no value
 A function with arguments and returning a values
1. A function with no arguments and no return value
Called function does not have any arguments

Not able to get any value from the calling function

Not returning any value

There is no data transfer between the calling function and called function.

2. A function with no arguments and a return value


Does not get any value from the calling function

Can give a return value to calling program

3. A function with an argument or arguments and returning no value


A function has argument/s

A calling function can pass values to function called , but calling function not
receive any value

Data is transferred from calling function to the called function but no data is
transferred from the called function to the calling function

Generally Output is printed in the Called function

A function that does not return any value cannot be used in an expression it can be
used only as independent statement.
4. A function with arguments and returning a values
Argument are passed by calling function to the called function

Called function return value to the calling function

Mostly used in programming because it can two way communication

Data returned by the function can be used later in our program for further
calculation.

You might also like