Document Details

SaintlySphene5524

Uploaded by SaintlySphene5524

Örebro University

2024

Pascal Rebreyend

Tags

programming C programming functions computer science

Summary

This document is a lecture on functions in programming, specifically in C. The lecture details concepts like parameters, arguments, and the use of functions in computer science, with examples included. The content relates to programmeringsteknik DT143G.

Full Transcript

Programmeringsteknik DT143G Lecture 6: Functions Pascal Rebreyend 21/11/2024 Pascal Rebreyend Programmeringsteknik DT143G Today’s contents Functions Parameters Arguments Pascal Rebreyend Programmeringsteknik DT143G W...

Programmeringsteknik DT143G Lecture 6: Functions Pascal Rebreyend 21/11/2024 Pascal Rebreyend Programmeringsteknik DT143G Today’s contents Functions Parameters Arguments Pascal Rebreyend Programmeringsteknik DT143G Why functions? We may want to reuse same code at different locations We want to have more readable code We want to use the idea of black-box up to how many lines of code we can deal in a convenient way?... Pascal Rebreyend Programmeringsteknik DT143G Functions We have already used functions: printf(),scanf()! We use () to characterize functions. Except very old and basic languages and specific ones, functions is a key feature of programming languages. Concept is closed to the mathematical one. Pascal Rebreyend Programmeringsteknik DT143G Functions Idea of a black box: We define it We use it We may only define OR use it. Definition: return_type func_name(parameter_list) {... } Pascal Rebreyend Programmeringsteknik DT143G Input, Output and side effects A function has: input(s): Can be nothing, or 1 or more parameters of different types output: A function can return a value which can stored in a variables (like sqrt,... ) (In C, not possible to return several values in a simple way). side-effects: All the changes to the environment. (Can be something on the screen like when using a printf, or modification to some variable/memory as we will see later). Caution: implicit modifications Pascal Rebreyend Programmeringsteknik DT143G Example #include #include double foo(int x, int y) { double res; res = pow(x, 3) + x*y + pow(y, 3); return res; } int main() { int n, m double r; printf("Enter n and m:"); scanf("%d%d", &n, &m); r = foo(n, m); printf("result = %lf\n", r); return 0; } Pascal Rebreyend Programmeringsteknik DT143G Scope of variables Scope of variable: Where can I use a given variable? The must-know when you use a programming language In a function, we can use local variables, and arguments (globals variables too) Arguments are passed by value: Modification are not seen outside the function We can use global variables But we should avoid global variables (unless some specific and particular case) Thus, the variale i in a function is not the same as the variable i of the calling function! Pascal Rebreyend Programmeringsteknik DT143G About functions: We may have specific cases We may have no parameter at all We can return void if nothing to return We may have no return statement. We should define a function before using it. (useful when c code is spread among different files, see later lectures) (Explain why we include files: Need to know the prototype of the functions used later) Pascal Rebreyend Programmeringsteknik DT143G Example #include long power(long x, long y); int main() { long n, m; printf("Enter n and m:"); scanf("%ld%ld", &n, &m); printf("n to power m = %ld\n\, power(n, m)); return 0; } long power(long x, long y) { int i; long result = 1; for (i = 1; i

Use Quizgecko on...
Browser
Browser